accepts_nested_attributes_for stops my sub-form rendering?
I have an association between 2 models, Business and Address. where the business has a registered_address. I have done this as follows.
class Business < ActiveRecord::Base
has_one :registered_address, :class_name => "Address", :foreign_key => :business_registered_address_id
accepts_nested_attributes_for :registered_address
end
class Address < ActiveRecord::Base
belongs_to :business
end
This association works fine for my purposes. When I render the form using:
= form_for @business do |form|
= form.inputs :name => "Registered address" do
= form.fields_for :registered_address do |address|
开发者_JS百科 = address.input :postcode
= address.input :line_1
= address.input :line_2
= address.input :line_3
= address.input :town
= address.input :county
Nothing is displayed, just an empty fieldset.
When I comment out the accepts_nested_attributes_for line in Business model, it displays (but doesn't save) all the fields correctly.
Can anyone see what I'm doing wrong?
Thank you
Write in your controller for this action (new
as I think)
def new
@business = Business.new
@business.build_registered_address
...
end
or in your form @business.registered_address.new
= form_for @business do |form|
= form.inputs :name => "Registered address" do
= form.fields_for :registered_address, @business.registered_address.new do |address|
= address.input :postcode
= address.input :line_1
= address.input :line_2
= address.input :line_3
= address.input :town
= address.input :county
form.fields_for renders its block for each object in @business.registered_adress. If your array is empty nothing in showed.
You can write, for example on your controller:
@bussines.registered_address.new
And then the app will display all the form
Hope this helps
精彩评论