rails Active Record 中的 attr_accessible [英] attr_accessible in rails Active Record

查看:30
本文介绍了rails Active Record 中的 attr_accessible的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 attr_accessible 指定我将公开模型中的哪些字段时,脚本/控制台也是如此吗?我的意思是我没有指定为 attr_accessible 的东西也不能通过控制台访问?

When I use the attr_accessible to specify which fields from my Model I will expose, is it true for script/console as well? I mean something that I didn't specify as attr_accessible won't be accessible as well through console ?

推荐答案

这仅适用于批量赋值.例如,如果您要在模型中设置 attr_protected :protected:

This is only true for mass assignment. For instance, if you were to set attr_protected :protected in your model:

>> Person.new(:protected => "test")
=> #<Person protected: nil>

相反,您可以使用 attr_accessible 将您想要的所有属性设置为可访问.

Conversely, you could set all attributes you want as accessible using attr_accessible.

但是,以下仍然有效:

>> person = Person.new
=> #<Person protected: nil>
>> person.protected = "test"
=> #<Person protected: "test">

这与控制器、视图等中的行为相同.attr_protected 仅防止大量分配变量,主要来自表单等.

This is the same behaviour as in controllers, views, etc. attr_protected only protects against mass assignment of variables, primarily from forms, etc.

这篇关于rails Active Record 中的 attr_accessible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆