VP.*_*VP. 10 ruby activerecord ruby-on-rails attr-accessible
当我使用它attr_accessible来指定我的模型I中的哪些字段将公开时,脚本/控制台也是如此?我的意思是我没有指定的东西attr_accessible也不能通过控制台访问?
Jos*_*man 19
这仅适用于质量分配.例如,如果您要attr_protected :protected在模型中设置:
>> Person.new(:protected => "test")
=> #<Person protected: nil>
Run Code Online (Sandbox Code Playgroud)
相反,您可以将所需的所有属性设置为可访问attr_accessible.
但是,以下内容仍然有效:
>> person = Person.new
=> #<Person protected: nil>
>> person.protected = "test"
=> #<Person protected: "test">
Run Code Online (Sandbox Code Playgroud)
这与控制器,视图等中的行为相同,attr_protected 仅防止变量的质量分配,主要来自表单等.
我找到了原因:
指定可以通过大规模分配设定模型的属性,如白名单new(attributes),update_attributes(attributes)或attributes=(attributes).这与attr_protected宏相反:
Mass-assignment will only set attributes in this list, to assign to the rest of
attributes you can use direct writer methods. This is meant to protect sensitive
attributes from being overwritten by malicious users tampering with URLs or forms.
If you‘d rather start from an all-open default and restrict attributes as needed,
have a look at `attr_protected`.
Run Code Online (Sandbox Code Playgroud)
所以这意味着它只是避免了质量分配,但我仍然可以设置一个值.