使用 Rails 3.1 :as => :admin 用于更新受 attr_accessible 保护的属性
阅读Rails 3.1 API 中的 attr_accessible 后,我看到那里有一个 as :admin
选项。我想知道两件事。
如果用户有管理员标志,我的控制器如何告诉我的模型该用户是管理员。
如果用户是所有者,我可以指定
:as =>所有者
在我的模型中,我的控制器如何通知我的模型他们是某个项目的所有者。
After reading about attr_accessible in the Rails 3.1 API, I see that there is an as :admin
option in there. I would like to know two things.
If the user has an admin flag, how do does my controller tell my model that the user is an admin.
If the user is an owner, can i specify
:as => owner
in my model, and once again how does my controller inform my model they are the owner of an item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有与模型的内置集成;您在
assign_attributes
调用中传入角色::as
参数默认为:default
,您可以传入任何您想要的符号。要将其集成到您的User
模型中,您可以为其指定一个名为role
的属性,然后执行以下操作:您还可以使用
:without_protection< 绕过保护/code>:
以类似的方式,
new
、create
、create!
、update_attributes
和update_attributes!
方法都遵循批量分配 安全。 Ruby on Rails 安全指南有 更多信息。There is no built-in integration with models; you pass in the role in the
assign_attributes
call:The
:as
parameter defaults to:default
, and you can pass in any symbol that you want. To integrate this into yourUser
model, you could give it an attribute calledrole
, and then do something like:You can also bypass the protection using
:without_protection
:In a similar way,
new
,create
,create!
,update_attributes
, andupdate_attributes!
methods all respect mass-assignment security. The Ruby on Rails guide on security has more info.对于这两种情况,您都可以按照最初声明它的方式传递它。例如:
如果您
这样做了,那么您将无法分配
credit_card
:但是,如果您声明它将是
:as =>; :admin
然后它允许更多信息:
http://www.enlightsolutions.com/articles/whats-new-in-edge-scoped-mass-assignment-in-rails-3-1
For both scenarios, you'd pass it in the same way that you declare it originally. So for example:
If you did
Then you won't be able to assign the
credit_card
:However, if you state that it will be
:as => :admin
then it allows itMore information:
http://www.enlightsolutions.com/articles/whats-new-in-edge-scoped-mass-assignment-in-rails-3-1
您想要作为特定用户访问的所有属性都应该正确定义。例如:
这对我来说是错误的。
但是当我将其修改为
当我使用时效果很好
all the attributes you want to access as a specific user should be defined properly. For example:
This showed error for me.
But when i modied it to
This worked fine when i used