여러분, 레일스 5는 레일스 4와 다른 뉘앙스를 갖고 있습니다. 제출 버튼을 클릭 할 때마다 프로필 사용자가 존재해야합니다. 및 프로필 사용자는 비워 둘 수 없습니다.. 나는 다른 있었다 한레일 5 - 중첩 모델 상위 모델이 하위 모델보다 먼저 저장되지 않기 때문에 롤백됩니다.
Puma starting in single mode...
* Version 3.7.0 (ruby 2.2.6-p396), codename: Snowy Sagebrush
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started POST "/users" for 192.168.0.31 at 2017-03-09 18:51:04 -0500
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
ActiveRecord::SchemaMigration Load (0.2ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JPKO+ppAYqwWS8tWeXhEtbUWynXREu9jYlF0KIlyPgUaabHSzjPZocSxCvr/WEm1r6wAQyT1CvA6hNkZWfPD3Q==", "user"=>{"username"=>"test", "password"=>"[FILTERED]", "user_type_id"=>"1", "profile_attributes"=>{"first_name"=>"123", "middle_name"=>"123", "last_name"=>"123", "email"=>"[email protected]", "phone_number"=>"1234567890", "cell_number"=>"1234567890"}}, "commit"=>"Create User"}
(0.1ms) BEGIN
(0.2ms) ROLLBACK
Rendering users/new.html.erb within layouts/application
Rendered users/_form.html.erb (112.5ms)
Rendered users/new.html.erb within layouts/application (118.7ms)
Completed 200 OK in 834ms (Views: 780.1ms | ActiveRecord: 2.2ms)
: 양식로드는 잘하지만 지금은 콘솔에 다음과 같은 출력으로 하위 모델을 저장하기 전에 상위 모델을 저장 실패 추론 무엇을위한, 중첩 된 모델 형태를 포함 이 관계에서 문제가 생겨서 프로젝트를 재건해야 할 필요가 있다고 생각합니다. 다음은이 문제를 해결 관련된 모든 코드입니다 : 나는 비슷한 문제가 발생했다
###############################################################################
### Users Model
###############################################################################
class User < ApplicationRecord
has_one :profile, inverse_of: :user
accepts_nested_attributes_for :profile, allow_destroy: true
end
###############################################################################
### Profile Model
###############################################################################
class Profile < ApplicationRecord
belongs_to :user, inverse_of: :profile
validates_presence_of :user
end
###############################################################################
### Users Controller
###############################################################################
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
@user.build_profile
end
# GET /users/new
def new
@user = User.new
@user.build_profile
end
# GET /users/1/edit
def edit
@user.build_profile
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:username, :password, :user_type_id, profile_attributes: [:id, :user_id, :first_name, :middle_name, :last_name, :phone_number, :cell_number, :email])
end
end
###############################################################################
### Form View
###############################################################################
<%= form_for(@user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<!--<li><%= debug f %></li>-->
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<% if params[:trainer] == "true" %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '2' %>
<% else %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '1' %>
<% end %>
</div>
<h2>Account Profile</h2>
<%= f.fields_for :profile do |profile| %>
<%#= profile.inspect %>
<div>
<%= profile.label :first_name %>
<%= profile.text_field :first_name %>
</div>
<div>
<%= profile.label :middle_name %>
<%= profile.text_field :middle_name %>
</div>
<div>
<%= profile.label :last_name %>
<%= profile.text_field :last_name %>
</div>
<div>
<%= profile.label :email %>
<%= profile.text_field :email %>
</div>
<div>
<%= profile.label :phone_number %>
<%= profile.telephone_field :phone_number %>
</div>
<div>
<%= profile.label :cell_phone %>
<%= profile.telephone_field :cell_number %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<%= debug params %>
<%= debug user %>
<%= debug user.profile %>
<% end %>
해결 방법이없는 것 같고 아무도 도움을 제공하지 않으므로 버그라고 가정 할 것입니다. 나는 시스템이 나를 허용 할 때 이것에 현상금을 부과 할 것이다. –