Rails 4 build_association
Rails 4 build_association
在我的 rails 4 应用程序中,我有一个 user
模型 has_one
profile
模型也 belongs_to
user
模型
这是我第一次使用嵌套资源,所以我有点迷失了
这是怎么回事
,用户创建一个帐户,成功通过电子邮件身份验证后,用户继续创建个人资料。问题是,我在使用 build_association 时遇到错误。我不确定问题是否真的出在这个问题上,还是我在参数上得到了空值(我认为不是)
在我的 routes.rb
resources :users do
resources :profiles
end
个人资料视图,new.html.erb
<%= form_for(@profile,url:{ :controller => 'profiles', :action => 'create'},html: {class: "ui large segment form error"}) do |f| %>
<%= render 'shared/form_error_messages', object: f.object%>
<div class="ui horizontal divider">Create a Profile</div>
<%= f.text_field :first_name, :placeholder => "First Name" , :required => true%>
<%= f.submit "Submit Profile", class: "ui submit button"%>
<% end %>
profiles_controller.rb
def new
@user = User.find(params[:user_id])
@profile = @user.build_profile
end
def create
@profile = @user.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
private
def profile_params
params.require(:profile).permit(:first_name)
end
当我提交表单时,我得到
NoMethodError in ProfilesController#create
undefined method `build_profile' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:9:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MFCANYra..",
"profile"=>{"first_name"=>"sample",
"commit"=>"Submit Profile",
"user_id"=>"1"}
尝试更改 profiles_controller.rb 中的创建操作,如下所示:-
def create
@user = User.find(params[:profile][:user_id])
@profile = @user.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
让我知道这是否有效,请尝试找出是否是其他原因导致的问题,谢谢。
在我的 rails 4 应用程序中,我有一个 user
模型 has_one
profile
模型也 belongs_to
user
模型
这是我第一次使用嵌套资源,所以我有点迷失了 这是怎么回事 ,用户创建一个帐户,成功通过电子邮件身份验证后,用户继续创建个人资料。问题是,我在使用 build_association 时遇到错误。我不确定问题是否真的出在这个问题上,还是我在参数上得到了空值(我认为不是)
在我的 routes.rb
resources :users do
resources :profiles
end
个人资料视图,new.html.erb
<%= form_for(@profile,url:{ :controller => 'profiles', :action => 'create'},html: {class: "ui large segment form error"}) do |f| %>
<%= render 'shared/form_error_messages', object: f.object%>
<div class="ui horizontal divider">Create a Profile</div>
<%= f.text_field :first_name, :placeholder => "First Name" , :required => true%>
<%= f.submit "Submit Profile", class: "ui submit button"%>
<% end %>
profiles_controller.rb
def new
@user = User.find(params[:user_id])
@profile = @user.build_profile
end
def create
@profile = @user.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
private
def profile_params
params.require(:profile).permit(:first_name)
end
当我提交表单时,我得到
NoMethodError in ProfilesController#create
undefined method `build_profile' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:9:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MFCANYra..",
"profile"=>{"first_name"=>"sample",
"commit"=>"Submit Profile",
"user_id"=>"1"}
尝试更改 profiles_controller.rb 中的创建操作,如下所示:-
def create
@user = User.find(params[:profile][:user_id])
@profile = @user.build_profile(profile_params)
if @profile.save
flash[:success] = "Profile has been created"
redirect_to root_url
else
render 'new'
end
end
让我知道这是否有效,请尝试找出是否是其他原因导致的问题,谢谢。