Ruby on rails 限制使用Ruby on Rails的用户类型可以访问属性

Ruby on rails 限制使用Ruby on Rails的用户类型可以访问属性,ruby-on-rails,attr,attr-accessible,Ruby On Rails,Attr,Attr Accessible,我正在创建一个论坛软件。我希望管理员和mods能够关闭某些主题 代码经过消毒,仅显示相关信息 型号 class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation, :bio has_many :topics, dependent: :destroy end class Topic < ActiveRecord::Base belongs_t

我正在创建一个论坛软件。我希望管理员和mods能够关闭某些主题

代码经过消毒,仅显示相关信息

型号

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :bio
  has_many :topics, dependent: :destroy
end

class Topic < ActiveRecord::Base
  belongs_to :user
  attr_accessible :name, :last_post_id, :content
end
主题的架构:关闭列确定主题的关闭状态

create_table "topics", :force => true do |t|
  t.datetime "created_at",                      :null => false
  t.datetime "updated_at",                      :null => false
  t.integer  "forum_id"
  t.string   "name"
  t.integer  "last_post_id"
  t.integer  "views"
  t.integer  "user_id"
  t.boolean  "closed",       :default => false
  t.text     "content"
end
我不愿意使用主题模型的用户
attr\u accessible:closed
,因为它容易受到恶意PUT请求的攻击(如果我错了,请纠正我)


Rails应用程序是否有一些方法可以访问和修改主题的
关闭的
列的值,而不使用
attr\u accessible
,这样只有MOD和管理员可以编辑它们?

我在谷歌上搜索并找到了这个

基本上,您正在寻找可访问的动态属性

如果你现在有

class Article < ActiveRecord::Base  
  attr_accessible :name, :content, :closed  
end  
类文章
class Article < ActiveRecord::Base  
  attr_accessible :name, :content, :closed  
end  
class Article < ActiveRecord::Base  
  attr_accessible :name, :content  
  private  
  def mass_assignment_authorizer  
    super + [:closed]  
  end  
end