Ruby-on-rails 无法批量分配受保护的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6163759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:15:40  来源:igfitidea点击:

Can't mass-assign protected attributes

ruby-on-railsnested-attributesmass-assignment

提问by Sanjay

Updating the code formatting for better viewing.

更新代码格式以更好地查看。

Folks,

各位,

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, 

end

结尾

-------------------- log output begin ------------------------------

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"=>"[email protected]", "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.idFROM usersWHERE (users.email= BINARY '[email protected]') LIMIT 1 SQL (1.0ms) ROLLBACK

-------------------- log output end ------------------------------

-------------------- 日志输出开始 --------------------------- ---

在 2011-05-28 11:43:27 -0700 开始为 127.0.0.1 POST "/users" 由 RegistrationsController#create 处理为 HTML 参数:{"utf8"=>"√","authenticity_token"=>"CEmdqlsmdYa6JqISsWkIhPix8 ", "user"=>{"email"=>"[email protected]", "password"=>"[FILT ERED]", "addresses_attributes"=>{"0"=>{"street_address1"=> "234 Pitkin Ct.", "zip"=>"12456"}}}, "commit"=>"Sign up"} 警告:不能批量分配受保护的属性:addresses_attributes SQL (0.0ms) BEGIN SQL (164.0 ms) 显示表
用户负载 (0.0ms) SELECT usersidusers哪里 ( users.email

-------------------- 日志输出结束 --------------------------- ---

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.

zip 存在于发布的数据中,并且发布的数据似乎格式正确。在网页表单上,我收到“地址 zip 不能为空”的错误消息。我一直在寻找导致“无法批量分配受保护的属性”警告的原因,但没有找到任何可以帮助我的东西。

Thanks for your thoughts and pointers.

感谢您的想法和指点。

-S

-S

回答by apneadiving

Have a look here and learn :)

看看这里并学习:)

http://railscasts.com/episodes/26-hackers-love-mass-assignment

http://railscasts.com/episodes/26-hackers-love-mass-assignment



Edit:

编辑:

Having accepts_nested_attributes_forin User model enables you to send the data to the Address model.

accepts_nested_attributes_for在用户模式使您能够将数据发送到地址模型。

Then, in the Address model, you have to set the requested attr_accessible

然后,在 Address 模型中,您必须设置请求的 attr_accessible

回答by BasicObject

Inside of SpecificModel(appfolder/app/model/specific_model.rb)

内部SpecificModelappfolder/app/model/specific_model.rb

Try using

尝试使用

attr_accessible :addresses_attributes, :another_attribute_to_make_mass_assignable, :another_attribute, etc.

attr_accessible :addresses_attributes, :another_attribute_to_make_mass_assignable, :another_attribute, etc.

回答by BvuRVKyUVlViVIc7

Nowadays (April 2013) you should start to use https://github.com/rails/strong_parameters

如今(2013 年 4 月)你应该开始使用https://github.com/rails/strong_parameters

回答by Raj Kumar

Just include the datafieldin the model as mentioned below

只需在模型中包含数据字段,如下所述

attr_accessible :addresses_attributes