【发布时间】:2014-04-30 17:44:16
【问题描述】:
我正在将 Devise 与两种用户类型一起使用,即 Employer 和 Candidate,它们彼此有很大不同。目前没有 STI,并且每个模型都有一个模型 + 设计认证。在实现 CanCan 进行授权时,我发现需要 current_candidate 或 current_employer 到 current_user 的别名。看来 current_user 只能使用一个别名,因此无法授权非别名用户类型进行任何操作。
class ApplicationController < ActionController::Base
alias_method :current_user, :current_candidate
#alias_method :current_user, :current_employer
#^ can't use both simultaneously!
end
由于只有 current_candidate 可以别名为用于 CanCan 的 current_ability 方法的 current_user,因此上述操作会导致雇主操作的授权失败。
def current_ability
@current_ability ||= ::Ability.new(current_user)
end
有没有办法有效地将两种用户类型别名为 CanCan 的 current_user?或者是否有某种类型的 before 方法可以在没有别名的情况下设置 current_user?如果需要,很高兴添加更多代码。
【问题讨论】:
标签: ruby-on-rails-4 devise cancan