在我的rails应用程序中设置不同的根路径

时间:2014-06-17 06:58:21

标签: ruby-on-rails-3.2

如何在rails 3中为routes.rb设置不同的根路径?我有两种类型的用户叫做admin和普通用户。我需要为用户提供两种不同的根路径。

目前我确实喜欢:

if current_user.admin == true
  root :to => 'dashboards#index'
else
  root :to => 'companies#index'
end

但是我收到了像

这样的错误
  

未定义的方法current_user

1 个答案:

答案 0 :(得分:0)

另一种解决方案就是路由到公司#index then ...

class CompaniesController < ApplicationController

  def index
    if current_user.admin == true
      redirect_to :controller => 'dashboards', :action => 'index'
    else
      # normal companies index action
    end
  end

end

您还可以将root引导到通用控制器中的操作,例如SessionsController,并将控制器重定向到仪表板或公司控制器。那可能更清洁。