获取无法批量分配受保护的属性:地址

时间:2012-02-11 22:52:02

标签: ruby-on-rails-3 polymorphic-associations has-one mass-assignment

当我尝试分配嵌套属性时,我在日志中收到以下内容。我已经扫描并尝试了所有可以找到的答案,但没有任何效果。

  

在2012-02-11 23:27:24 +0100开始发布“/ admin / care_homes”for 127.0.0.1       由Admin :: CareHomesController处理#create as HTML       参数:{“utf8”=>“✓”,“authenticity_token”=>“Zymx7VQU1mi + ho5T + Ups6cvHavpE4ClU6g1QFi + Y3z8 =”,“care_home”=> {“organisation_name”=>“”,“cqc_id”= >“”,“csa_id”=>“”,“地址”=> {“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”=>“”}}     用户负载(0.4ms)SELECT users。* FROM users WHERE usersid = 4 LIMIT 1

     

警告:无法批量分配受保护的属性:地址

我有一个CareHome的STI<服务。地址是一种多态关系。

在服务中我有:

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

在CareHomeController中#new / create我有

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

如果我将地址添加到attr_accessible,我会收到错误:

  

地址(#2560574700)预期,获得ActiveSupport :: HashWithIndifferentAccess(#2157282280)

我的Rails版本是3.1.1。

我想它一定是微妙的东西,但我已经没有想法尝试了。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

你写attr_accesssible是否有任何理由。

注释掉该行并尝试