0

ブログ アプリに宣言型承認を実装しました。これで、管理者、認証済みユーザー、およびゲスト ユーザー用にそれぞれ 3 つのレイアウトができました。そのため、特定の時間にアプリを使用しているユーザーのタイプを確認する必要があります。ユーザーモデル、ロールモデル、割り当てモデルがあります。

ユーザー.rb

class User < ActiveRecord::Base

  attr_accessible :login, :email, :password, :password_confirmation, :role_ids

  has_many :articles
  has_many :comments
  has_many :assignments

  has_many :roles, :through => :assignments

  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end

  acts_as_authentic do |c|
    c.login_field = :login
  end

  def deliver_password_reset_instructions!
    reset_perishable_token!
    Notifier.deliver_password_reset_instructions(self)
  end

end

割り当て.rb

class Assignment < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

Role.rb

class Role < ActiveRecord::Base
  attr_accessible :name
  has_many :assignments
  has_many :users, :through => :assignments
end

解決策はありますか?

4

1 に答える 1

0

次のgem を使用して構造を簡素化できます: https://github.com/platform45/easy_roles github の指示に従い、そこに記載されているようにモデルを変更します。それは本当に簡単で、ほんの数ステップです。ユーザーモデルが必要なだけです。

さらに、cancan gem ( https://github.com/ryanb/cancan ) をお勧めします。

easy_roles と cancan は、ロールとパーミッションを非常に簡単に定義するための良い組み合わせです!

于 2013-05-03T10:53:32.187 に答える