嵌套属性未允许的参数我有一个Bill对象,它有很多Due对象。该Due对象也属于Person。我想要一个可以在一个页面中创建Bill及其子项的表单Dues。我正在尝试使用嵌套属性创建表单,类似于此Railscast中的表单。相关代码如下:due.rbclass Due < ActiveRecord::Base
belongs_to :person
belongs_to :billendbill.rbclass Bill < ActiveRecord::Base
has_many :dues, :dependent => :destroy
accepts_nested_attributes_for :dues, :allow_destroy => trueendbills_controller.rb # GET /bills/new
def new @bill = Bill.new 3.times { @bill.dues.build }
end票据/ _form.html.erb <%= form_for(@bill) do |f| %>
<div class="field">
<%= f.label :company %><br />
<%= f.text_field :company %>
</div>
<div class="field">
<%= f.label :month %><br />
<%= f.text_field :month %>
</div>
<div class="field">
<%= f.label :year %><br />
<%= f.number_field :year %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<%= f.fields_for :dues do |builder| %>
<%= render 'due_fields', :f => builder %>
<% end %>
<% end %>票据/ _due_fields.html.erb<div>
<%= f.label :amount, "Amount" %>
<%= f.text_field :amount %>
<br>
<%= f.label :person_id, "Renter" %>
<%= f.text_field :person_id %></div>更新到bills_controller.rb 这有效!def bill_params
params .require(:bill)
.permit(:company, :month, :year, dues_attributes: [:amount, :person_id]) end在页面上呈现正确的字段(尽管还没有下拉列表Person),并且提交成功。但是,没有子项会被保存到数据库中,并且服务器日志中会抛出错误:Unpermitted parameters: dues_attributes在错误发生之前,日志显示如下:Started POST "/bills" for 127.0.0.1 at 2013-04-10 00:16:37 -0700Processing by BillsController#create as HTML<br>Parameters: {"utf8"=>"✓", "authenticity_token"=>"ipxBOLOjx68fwvfmsMG3FecV/q/hPqUHsluBCPN2BeU=",
"bill"=>{"company"=>"Comcast", "month"=>"April ", "year"=>"2013", "dues_attributes"=>{"0"=>{"amount"=>"30", "person_id"=>"1"}, "1"=>{"amount"=>"30", "person_id"=>"2"},
"2"=>{"amount"=>"30", "person_id"=>"3"}}}, "commit"=>"Create Bill"}Rails 4有没有变化?
3 回答
慕尼黑5688855
TA贡献1848条经验 获得超2个赞
或者你可以简单地使用
def question_params params.require(:question).permit(team_ids: [])end
- 3 回答
- 0 关注
- 585 浏览
添加回答
举报
0/150
提交
取消