I am getting the following in the log when I try to assign a nested attribute. I have scanned and tried all the answers that I can find, but nothing works.
Started POST "/admin/care_homes" for 127.0.0.1 at 2012-02-11 23:27:24 +0100 Processing by Admin::CareHomesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Zymx7VQU1mi+ho5T+Ups6cvHavpE4ClU6g1QFi+Y3z8=", "care_home"=>{"organisation_name"=>"", "cqc_id"=>"", "csa_id"=>"", "address"=>{"street_address"=>"", "address_line_two"=>"", "city"=>"", "county_id"=>"1", "postcode"=>""}, "registered_manager"=>"", "telephone_number"=>"", "website"=>"", "region_id"=>"1", "authority_id"=>"1", "provider_id"=>"11789", "details"=>"", "directions"=>""}} User Load (0.4ms) SELECT
users
.* FROMusers
WHEREusers
.id
= 4 LIMIT 1WARNING: Can't mass-assign protected attributes: address
I have an STI of CareHome < Service. Address is a polymorphic relationship.
In service I have:
class Service < ActiveRecord::Base
paginates_per 15
image_accessor :home_image
has_one :address, :as => :addressable, :validate => true
has_one :county, :through => :address
attr_accessible :organisation_name, :cqc_id, :csa_id, :registered_manager,
:telephone_number,
:website, :region_id, :authority_id, :provider_id,
:details, :directions, :home_image, :retained_home_image,
:county, :address_attributes
accepts_nested_attributes_for :address
In CareHomeController#new/create I have
def new
@care_home = CareHome.new
@care_home.build_address
end
def create
@care_home = CareHome.new(params[:care_home])
if @care_home.save
redirect_to admin_care_home_path(@care_home), :notice => 'Saved'
else
render 'new'
end
end
class Address < ActiveRecord::Base
attr_accessible :id, :street_address, :address_line_two, :city, :county_id, :postcode, :country_id, :addressable_id, :addressable_type
belongs_to :addressable, :polymorphic => true
belongs_to :county
If I add :address to the attr_accessible I get the error:
Address(#2560574700) expected, got ActiveSupport::HashWithIndifferentAccess(#2157282280)
My Rails version is 3.1.1.
I guess it must be something subtle, but I have run out of ideas to try. Any help is appreciated!