如何在 Ruby/Rails 中指定私有或受保护的属性?
如何在 Ruby/Rails 中指定私有或受保护的属性?
所有数据库字段都自动具有属性,并且不需要在模型中定义吗?
有推荐的教程吗?
在 Rails 3.0.7 中工作。
How do you designate private or protected attributes in Ruby/Rails?
Are all DB fields automatically attributes, and don't need to be defined in the Model?
Any recommended tutorials?
Working in Rails 3.0.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
attr_protected
、attr_accessible
或attr_readonly
attr_protected, attr_readonly 和 attr_accessible 宏控制批量分配接受的内容。如果您不熟悉这三个宏之间的区别,请阅读这些链接。
ActiveRecord 模型的文档:
http://api.rubyonrails.org/classes/ActiveRecord/Base。 html
http://apidock.com/rails/ActiveRecord/Base
You can use
attr_protected
,attr_accessible
orattr_readonly
The attr_protected, attr_readonly and attr_accessible macros control what is accepted for mass-assignment. Read those links if you’re not familiar with the difference between those three macros.
Documentation of ActiveRecord model:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
http://apidock.com/rails/ActiveRecord/Base
标题与问题不符。
是的,数据库字段自动成为属性(取决于您对属性的含义;它们不只是像
attr_accessor
那样的@column_name
)。您可以使用
attr_accessible
提供某种程度的可访问性和attr_protected
,但这是用于批量分配,而不是一般访问。The title doesn't match the question.
Yes, DB fields are automatically attributes (depending on what you mean by attribute; they're not simply
@column_name
as withattr_accessor
).You can provide some level of accessibility by using
attr_accessible
andattr_protected
, but that's for mass-assignment, not general access.