Rails 4:accepts_nested_attributes_for和大量分配 [英] Rails 4: accepts_nested_attributes_for and mass assignment
问题描述
我正在尝试在Rails 4中复制 railscast#196 .但是,我正在遇到一些问题.
I am trying to reproduce railscast #196 in Rails 4. However, I'm experiencing some problems.
在我的示例中,我尝试生成一个电话簿-每个人可以有多个电话号码
In my example I try to generate a Phonebook - each Person could have multiple PhoneNumbers
这些是我控制器的重要组成部分:
These are important parts of my controller:
class PeopleController < ApplicationController
def new
@person = Person.new
3.times{ @person.phones.build }
end
def create
@person = Person.create(person_params)
@person.phones.build(params[:person][:phones])
redirect_to people_path
end
private
def person_params
params.require(:person).permit(:id, :name, phones_attributes: [ :id, :number ])
end
end
这是我的新观点
<h1>New Person</h1>
<%= form_for :person, url: people_path do |f| %>
<p>
<%= f.label :name %> </ br>
<%= f.text_field :name %>
</p>
<%= f.fields_for :phones do |f_num| %>
<p>
<%= f_num.label :number %> </ br>
<%= f_num.text_field :number %>
</p>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
不必说我的个人模型中有has_many :phones
和accepts_nested_attributes_for :phones
,而电话模型中有belongs_to :person
.
needless to say i have has_many :phones
and accepts_nested_attributes_for :phones
in the my person model and belongs_to :person
in the phone model.
我遇到以下问题:
- 新格式中只有3个电话号码字段
- 提交表单时出现错误:
ActiveModel :: ForbiddenAttributesError
ActiveModel::ForbiddenAttributesError
在线
@person.phones.build(params[:person][:phones])
参数:
{"utf8"=>"✓",
"authenticity_token"=>"l229r46mS3PCi2J1VqZ73ocMP+Ogi/yuYGUCMu7gmMw=",
"person"=>{"name"=>"the_name",
"phones"=>{"number"=>"12345"}},
"commit"=>"Save Person"}
原则上,我希望将整个事情作为表单对象来完成,但是我想,如果我什至没有用accepts_nested_attributes来做到这一点,我就没有机会作为表单对象来做:(
In principle I would like to do this whole thing as a form object, but I think if I don't even get it with accepts_nested_attributes, I have no chance to do it as a form object :(
推荐答案
要在视图中获取三部手机,请将form_for :person
更改为form_for @person
(您要使用在此处构建的对象),如下所示:
In order to get three phones in the view change form_for :person
to form_for @person
(you want to use the object you've built here) as follows:
<%= form_for @person, url: people_path do |f| %>
这也应该修复ForbiddenAttributes
错误.
您的create
动作可能是:
def create
@person = Person.create(person_params)
redirect_to people_path
end
更新:
<%= form_for :person do |f| %>
为Person
模型创建通用形式,并且不知道您对特定对象(在本例中为new
操作中的@person
)应用的其他详细信息.您已经将三个phones
附加到@person
对象,并且@person
与:person
不同,这就是为什么您在视图中没有看到三个电话字段的原因.请进一步参考: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for 详细信息.
<%= form_for :person do |f| %>
creates a generic form for the Person
model and is not aware of the additional details you apply to a specific object (in this case @person
in your new
action). You've attached three phones
to the @person
object, and @person
is not the same as :person
which is why you didn't see three phone fields in your view. Please reference: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for for further details.
这篇关于Rails 4:accepts_nested_attributes_for和大量分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!