无法批量分配受保护的属性 [英] Can't mass-assign protected attributes
问题描述
更新代码格式以更好地查看.
Updating the code formatting for better viewing.
伙计,
我已经看过一段时间了,但是我不明白这里可能发生什么.我正在使用Devise.
I have been looking at this for sometime but I don't understand what could be messing up here. I am using Devise.
class User < ActiveRecord::Base
has_many :addresses
accepts_nested_attributes_for :addresses
# Other stuff here
end
class Address < ActiveRecord::Base
belongs_to :user
validates_presence_of :zip #:street_address1,
结束
--------------------日志输出开始------------------------ ------
-------------------- log output begin ------------------------------
在127.0.0.1开始POST"/用户"
2011-05-28 11:43:27 -0700处理
通过RegistrationsController#create作为
HTML参数:{"utf8" =>√",
"authenticity_token" =>"CEmdqlsmdYa6Jq0iIf5KAxxISsUCREIrFNXWkP80nhk =",
用户" => {电子邮件" =>"a2@gmail.com",
"password" =>"[FILT ERED]",
"addresses_attributes" => {"0" => {"street_address1" =>"234
Pitkin Ct.," zip"=>" 12456}}},
"commit" =>注册"}警告:不能
批量分配受保护的属性:
addresss_attributes SQL(0.0ms)
BEGIN SQL(164.0ms)显示表
用户负载(0.0ms)SELECT users
.id
从users
位置(users
.email
=
BINARY'a2@gmail.com')限制1 SQL
(1.0ms)回滚
Started POST "/users" for 127.0.0.1 at
2011-05-28 11:43:27 -0700 Processing
by RegistrationsController#create as
HTML Parameters: {"utf8"=>"√",
"authenticity_token"=>"CEmdqlsmdYa6Jq0iIf5KAxxISsUCREIrFNXWkP80nhk=",
"user"=>{"email"=>"a2@gmail.com",
"password"=>"[FILT ERED]",
"addresses_attributes"=>{"0"=>{"street_address1"=>"234
Pitkin Ct.", "zip"=>"12456"}}},
"commit"=>"Sign up"} WARNING: Can't
mass-assign protected attributes:
addresses_attributes SQL (0.0ms)
BEGIN SQL (164.0ms) SHOW TABLES
User Load (0.0ms) SELECT users
.id
FROM users
WHERE (users
.email
=
BINARY 'a2@gmail.com') LIMIT 1 SQL
(1.0ms) ROLLBACK
--------------------日志输出结束------------------------ ------
-------------------- log output end ------------------------------
已发布的数据中存在zip,并且已发布的数据似乎格式正确.在网页表单上,我收到地址压缩不能为空"的错误消息.我一直在寻找导致无法批量分配受保护的属性"警告的原因,但没有发现任何对我有帮助的东西.
The zip is present in the data posted and the posted data seems to be formatted properly. On the web page form I am getting the error that "Addresses zip can't be blank". I have dug around for what causes the "Can't mass-assign protected attributes" warning but haven't found anything that will help me.
感谢您的想法和指导.
-S
推荐答案
在这里看看并学习:)
http://railscasts.com/episodes/26-hackers-love-mass-assignment
在用户模型中具有accepts_nested_attributes_for
可使您将数据发送到地址模型.
Having accepts_nested_attributes_for
in User model enables you to send the data to the Address model.
然后,在地址"模型中,您必须设置请求的attr_accessible
Then, in the Address model, you have to set the requested attr_accessible
这篇关于无法批量分配受保护的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!