Rails 3 nested forms won't work
My accommodation
model:
class Accommodation < Active开发者_如何转开发Record::Base
...
has_one :address
...
accepts_nested_attributes_for :address
...
end
My address
model:
class Address < ActiveRecord::Base
belongs_to :accommodation
end
My accommodation _form.html.haml
partial
=form_for [:panel,@accommodation], :html => {:class=>'accommodation'} do |f|
%fieldset
%legend
Adres
=f.fields_for :address do |address_f|
=address_f.label :street, "Ulica"
=address_f.text_field :street
%div
Problem is the field for street will not show. It shows when I change :address to @accommodation.build_address but then the form is not accepted.
How to do it properly to work?Found a solution:
fields_for
needs to look like this:
=f.fields_for :address_attributes, @accommodation.address do |address_f|
It works perfectly now.
try it
=f.fields_for :address, @accomodation.address.new do |address_f|
精彩评论