嵌套屬性未允許的參數我有一個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 關注
- 619 瀏覽
添加回答
舉報
0/150
提交
取消