3

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.* FROM users WHERE users.id = 4 LIMIT 1

WARNING: 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!

Geoff
  • 966
  • 7
  • 4

1 Answers1

0

Is there any reasons why you write attr_accesssible.

comment out that line and try

DeathHammer
  • 650
  • 6
  • 11
  • Because the documentation says to. I just tried taking it out and I get the same error. – Geoff Feb 12 '12 at 09:26
  • I think I have found it. Using `render :partial => "form", :locals{:f => f creates a form with field names 'address' If I use `render "form", :f => f Then it creates a form with field names 'address_attributes' and that works OK. Not sure why there should be a difference? – Geoff Feb 13 '12 at 11:22