2

I tried to follow this answer to a similar question but in my case I'm using Rolify.

The problem I'm running into is in passthrough_controller.rb where I can't access the Rolify .has_role? function:

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

I have logged in using a user that I know has an admin role but it still redirects me to my else clause.

I couldn't find anything on the Rolify github page on how to do this.

Any help would be much appreciated

4

2 回答 2

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
于 2013-10-30T14:29:01.333 回答
0

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

我只需要在 my前面restaurants#index加上 a/所以我的代码应该是这样的:

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

我一开始没有前缀,因为这就是我在routes.rb( root :to => 'restaurants#index')中定义我的根的方式

于 2012-07-10T22:47:52.060 回答