मेरे पास एक Bill
ऑब्जेक्ट है, जिसमें कई Due
ऑब्जेक्ट हैं। Due
वस्तु भी एक के अंतर्गत आता है Person
। मुझे एक ऐसा फॉर्म चाहिए, जो एक पेज में Bill
और उसके बच्चों को बना सके Dues
। मैं नेस्टेड विशेषताओं का उपयोग करके एक फॉर्म बनाने की कोशिश कर रहा हूं, जो इस रेलकास्ट में लोगों के समान है ।
प्रासंगिक कोड नीचे सूचीबद्ध है:
due.rb
class Due < ActiveRecord::Base
belongs_to :person
belongs_to :bill
end
bill.rb
class Bill < ActiveRecord::Base
has_many :dues, :dependent => :destroy
accepts_nested_attributes_for :dues, :allow_destroy => true
end
bills_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>
यह काम करता है बिल_कंट्रोलर को अपडेट करें !
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 -0700
Processing 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"}
क्या रेल्स 4 में कुछ बदलाव हुआ है?