这真让我抓狂。我正在尝试使用 devise 设置权威,并且一切正常,直到我尝试设置默认角色。就我而言,默认角色是用户。当我尝试登录时,出现以下错误:
设计中的 ArgumentError::SessionsController#new
app/models/user.rb:26:in `set_default_role'
这是我的user.rb:
class User < ActiveRecord::Base
enum role: [:user, :gig, :band, :venue, :promoter, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
belongs_to :role
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :profile, presence: true,
uniqueness: true,
format: {
with: /[a-zA-Z0-9-]+/,
message: 'Must be formated correctly.'
}
before_create :set_default_role
has_many :gigs
private
def set_default_role
self.role ||= Role.find_by_name('user')
end
def gravatar_url
stripped_email = email.strip
downcased_email = stripped_email.downcase
hash = Digest::MD5.hexdigest(downcased_email)
"http://gravatar.com/avatar/#{hash}"
end
end
这是我的rold.rb:
class Role < ActiveRecord::Base
has_many :users
end
有任何想法吗?