使用 Rails 3.1 :as => :admin 用于更新受 attr_accessible 保护的属性

发布于 2024-12-25 23:52:29 字数 384 浏览 0 评论 0原文

阅读Rails 3.1 API 中的 attr_accessible 后,我看到那里有一个 as :admin 选项。我想知道两件事。

  1. 如果用户有管理员标志,我的控制器如何告诉我的模型该用户是管理员。

  2. 如果用户是所有者,我可以指定 :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.

  1. If the user has an admin flag, how do does my controller tell my model that the user is an admin.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2025-01-01 23:52:29

没有与模型的内置集成;您在 assign_attributes 调用中传入角色:

@project.assign_attributes(params[:project], :as => :admin)

:as 参数默认为 :default,您可以传入任何您想要的符号。要将其集成到您的 User 模型中,您可以为其指定一个名为 role 的属性,然后执行以下操作:

@project.assign_attributes(params[:project], :as => current_user.role.to_sym)

您还可以使用 :without_protection< 绕过保护/code>:

@project.assign_attributes(params[:project], :without_protection => true)

以类似的方式,newcreatecreate!update_attributesupdate_attributes! 方法都遵循批量分配 安全。 Ruby on Rails 安全指南更多信息

There is no built-in integration with models; you pass in the role in the assign_attributes call:

@project.assign_attributes(params[:project], :as => :admin)

The :as parameter defaults to :default, and you can pass in any symbol that you want. To integrate this into your User model, you could give it an attribute called role, and then do something like:

@project.assign_attributes(params[:project], :as => current_user.role.to_sym)

You can also bypass the protection using :without_protection:

@project.assign_attributes(params[:project], :without_protection => true)

In a similar way, new, create, create!, update_attributes, and update_attributes! methods all respect mass-assignment security. The Ruby on Rails guide on security has more info.

仙气飘飘 2025-01-01 23:52:29

对于这两种情况,您都可以按照最初声明它的方式传递它。例如:

class User < ActiveRecord::Base
  attr_accessible :name
  attr_accessible :credit_card, :as => :admin
end

如果您

user = User.new(:name => "John", :credit_card => "1234123412341234")

这样做了,那么您将无法分配 credit_card

user.attributes # {:name => "John", :credit_card => nil} 

但是,如果您声明它将是 :as =>; :admin 然后它允许

user = User.new({:name => "John", :credit_card => "1234123412341234"}, :as => :admin)
user.attributes # {:name => "John", :credit_card => "1234123412341234"} 

更多信息:

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:

class User < ActiveRecord::Base
  attr_accessible :name
  attr_accessible :credit_card, :as => :admin
end

If you did

user = User.new(:name => "John", :credit_card => "1234123412341234")

Then you won't be able to assign the credit_card:

user.attributes # {:name => "John", :credit_card => nil} 

However, if you state that it will be :as => :admin then it allows it

user = User.new({:name => "John", :credit_card => "1234123412341234"}, :as => :admin)
user.attributes # {:name => "John", :credit_card => "1234123412341234"} 

More information:

http://www.enlightsolutions.com/articles/whats-new-in-edge-scoped-mass-assignment-in-rails-3-1

你是暖光i 2025-01-01 23:52:29

您想要作为特定用户访问的所有属性都应该正确定义。例如:

    class User < ActiveRecord::Base
    attr_accessible :name
    attr_accessible :credit_card, :as => :admin
    end

这对我来说是错误的。
但是当我将其修改为

    class User < ActiveRecord::Base
    attr_accessible :name
    attr_accessible :name, :credit_card, :as => :admin
    end

当我使用时效果很好

    @user.update_attributes(params[:user], :as => :admin)

all the attributes you want to access as a specific user should be defined properly. For example:

    class User < ActiveRecord::Base
    attr_accessible :name
    attr_accessible :credit_card, :as => :admin
    end

This showed error for me.
But when i modied it to

    class User < ActiveRecord::Base
    attr_accessible :name
    attr_accessible :name, :credit_card, :as => :admin
    end

This worked fine when i used

    @user.update_attributes(params[:user], :as => :admin)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文