如何使用Rolify基于角色重定向root?

时间:2012-07-10 20:21:12

标签: ruby-on-rails authentication device cancan

我尝试关注this answer类似问题但在我的情况下我使用Rolify

我遇到的问题是passthrough_controller.rb,我无法访问Rolify .has_role?功能:

class PassthroughController < ApplicationController
  def index
    if current_user.has_role? :admin
      redirect_to 'restaurants#index'
    else
      redirect_to 'http://www.google.com'
    end
  end
end

我使用我知道具有管理员角色的用户登录但仍然将我重定向到我的else子句。

我无法在Rolify github页面上找到有关如何执行此操作的任何内容。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

解决方案实际上非常简单。

我只需要在我的restaurants#index前加/作为前缀,这样我的代码应该是这样的:

  def index
    if current_user.has_role? :admin
      redirect_to '/restaurants#index'
    else
      redirect_to 'http://www.google.com'
    end
  end

我在开头没有前缀,因为这是我在routes.rbroot :to => 'restaurants#index')中定义根的方式

答案 1 :(得分:0)

看起来您可以使用渲染来执行此操作,而不是使用路由或重定向。

def index
    if current_user.has_role? :admin
      render "admin" # where admin.html.erb is in the same folder as index.html.erb
    else
      # do nothing - render default view
    end
end