Ruby on rails 如何基于当前用户角色链接到路径?

Ruby on rails 如何基于当前用户角色链接到路径?,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,我有一个使用Desive、cancan和rolify的应用程序,可以在登录时重定向到特定于其角色的页面。我想弄明白的是,我如何在应用程序的其他地方链接到同一路径,而不仅仅是作为重定向 class ApplicationController < ActionController::Base protect_from_forgery rescue_from CanCan::AccessDenied do |exception| redirect_to root_path, :

我有一个使用Desive、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
class ApplicationControllerexception.message
结束
在(资源)的路径中签名后定义
case current_user.roles.first.name
当“管理员”
用户路径
当‘阴’
内容(音)(路)
当‘杨’
内容之路
当‘阴阳之光’
内容(阴)(阳)(光)(光)路)
当‘阴阳亲’
内容\阴\阳\亲\路
其他的
根路径
结束
结束
结束

基本上,我正在寻找如何访问当前用户=>role=>path或当前用户角色路径

您可以使用控制器中的
helper\u方法
宏来定义任何控制器方法,使其也充当助手方法。对于您的情况:

ApplicationController

helper_method :after_sign_in_path_for

您是否只希望上述相同方法可用于视图/控制器中的
link_to
?如果是这样,您可以将其定义为帮助器方法,方法是将此宏添加到您的应用程序控制器:
helper\u方法:在
的_路径_中签名之后。这非常有效。我知道我错过了一些简单的东西。谢谢你。我把这个建议写进了一个答案,这样你就可以接受它,并把它从没有答案的名单中去掉。谢谢