如何基于current_user角色链接到路径?

时间:2013-09-20 01:33:29

标签: ruby-on-rails ruby-on-rails-3 devise

我有一个使用devise,cancan和rolify的应用程序,可以在登录时重定向到特定于其角色的页面。我想弄清楚的是我如何在应用程序的其他地方链接到相同的路径,而不仅仅是作为重定向?

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_path, :alert => exception.message
  end

  def after_sign_in_path_for(resource)
    case current_user.roles.first.name
      when 'admin'
        users_path
      when 'yin'
        content_yin_path
      when 'yang'
        content_yang_path
      when 'yin_yang_lite'
        content_yin_yang_lite_path
      when 'yin_yang_pro'
        content_yin_yang_pro_path
      else
        root_path
    end
  end

end

基本上,我正在寻找如何到达current_user =&gt; role =&gt; path或current_user_role_path

1 个答案:

答案 0 :(得分:0)

您可以使用控制器中的helper_method宏定义任何控制器方法,以充当辅助方法。对于你的情况:

ApplicationController

helper_method :after_sign_in_path_for