获取不能批量分配受保护的属性:地址
我在日志中获取以下内容时尝试分配嵌套属性。我扫描并尝试了所有我能找到的答案,但没有任何效果。获取不能批量分配受保护的属性:地址
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
我有一个CareTI的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#新/创建我有
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我得到的错误:
Address(#2560574700) expected, got ActiveSupport::HashWithIndifferentAccess(#2157282280)
我的Rails版本是3.1.1。
我想这一定是微妙的,但我已经用尽了想法。任何帮助表示赞赏!
有什么理由你写attr_accesssible。
注释掉该行并尝试
由于文档说明。我只是试着把它拿出来,我得到了同样的错误。 – Geoff 2012-02-12 09:26:43
告诉我们你的新表格 – DeathHammer 2012-02-12 11:49:04
我想我已经找到它了。 使用 '渲染:局部=> “形式”,:当地人{:F =>˚F 创建具有字段名 '地址' 形式如果我使用 '呈现 “形式”,:F =>˚F 然后它创建一个字段名称为“address_attributes”的表单,并且工作正常。 不确定为什么应该有区别? – Geoff 2012-02-13 11:22:28