Do I need to keep the fields for my polymorphic address in the view to make accepts_nested_attributes_for work?
I have a problem where my classes are posted to the server but not saved and I can't figure out why and would appreciate some help in the matter.
First of all take my account class it has a reference to the address and defines both a billing and a delivery address.
class Account < ActiveRecord::Base
has_many :addresses, :as => :addressable, :dependent => :destroy
has_one :billing_address, :as => :addressable
has_one :delivery_address, :as => :addressable
accepts_nested_attributes_for :billing_address,
:allow_destroy => true,
:reject_if => missing_attrs?('street_one', 'zip', 'city', 'country_id')
accepts_nested_attributes_for :delivery_address,
:allow_destroy => true,
:reject_if => missing_attrs?('street_one', 'zip', 'city', 'country_id')
end
Now my address classes look like follows
class Address < ActiveRecord::Base
belongs_to :country
belongs_to :addressable, :polymorphic => true
validates_inclusion_of :type, :in => %w(BillingAddress DeliveryAddress ), :message => "Please speficy either billing or delivery address"
attr_accessor :street_one, :street_two, :zip, :city, :country_id, :region
end
class BillingAddress < Address
end
class DeliveryAddress < Address
end
Then I have a view with the following form and I must say it does look perfect in the browser, all the fields are there and the names are correc with the array elelments and all.
=semantic_form_for [:admin,@account] do |f|
= render 'shared/errors', :target => @account
=f.inputs
=f.semantic_fields_for :billing_address do |address|
=address.inputs name:"Billing Address" do
=address.input :street_one
=address.input :street_two
=address.input :city
=address.input :zip
=address.input :region
=address.input :country
=f.semantic_fields_for :delivery_address do |address|
=address.inputs name:"Delivery Address" do
=address.input :street_one
=address.input :street_two
=address.input :city
=address.input :zip
=address.input :region
=address.input :country
=f.semantic_fields_for :users do |user|
=user.inputs name:"Default user" do
=user.input :email
=us开发者_如何转开发er.input :password
=f.submit
If I check this with raise params.to_yaml
it does look correct though the polymorphic fields for addressable and type is missing. I guess since I build this in the controller I need to keep them in the view before I post them back to the controller before update and create. I read this post but can't figure out exactly what it does.
Can I have your best suggestions please?
I added the missing fields in the form and now it's working perfectly!
精彩评论