Ruby on rails Rails、主动管理、设计、路由

Ruby on rails Rails、主动管理、设计、路由,ruby-on-rails,ruby,devise,routes,cancancan,Ruby On Rails,Ruby,Devise,Routes,Cancancan,我一直在与Rails、Active Addmin和cancancan合作。除了一件事,一切都很好。最近,我为我的管理员类型用户和客户端添加了单独的名称空间 在此更改之前,我将所有经过身份验证的用户重定向到相同的活动管理仪表板(routes.rb): 目前,我需要以某种方式添加附加条件,以检查经过身份验证的用户是否具有角色admin或client 我的想法是做这样的东西: devise_scope :user do authenticated :user do if curr

我一直在与Rails、Active Addmin和cancancan合作。除了一件事,一切都很好。最近,我为我的管理员类型用户和客户端添加了单独的名称空间

在此更改之前,我将所有经过身份验证的用户重定向到相同的活动管理仪表板(routes.rb):

目前,我需要以某种方式添加附加条件,以检查经过身份验证的用户是否具有角色adminclient

我的想法是做这样的东西:

devise_scope :user do
    authenticated :user do 
      if current_user.role?(:Architect) || current_user.role?(:Admin) 
        root :to => 'admin/dashboard#index', as: :authenticated_root
      else 
        root :to => 'clients/dashboard#index', as: :authenticated_client
      end
    end
    unauthenticated :user do
      root :to => 'pages#index', as: :unauthenticated_root
    end
  end
但我得到了一个错误:未定义的局部变量或方法“current\u user”
有人知道我如何检查用户在路由中的角色吗?有更好的方法吗?

页面控制器:

class PageController < ApplicationController

  before_filter :check_route, :only => [:index]

  def check_route
    return unless user_signed_in?
    if current_user.role?(:Architect) || current_user.role?(:Admin)
      redirect_to :controller => 'admin/dashboard', :action => 'index'
    else
      redirect_to :controller => 'clients/dashboard', :action => 'index'
    end
  end
end

根目录是负责路由定义的配置文件,您不能访问配置文件中的任何会话变量

如果您想在登录后重定向用户,可以使用
designe::sessioncontroller

class SessionsController < Devise::SessionsController

    def after_sign_in_path_for(resource)
        if resource.role?(:Architect) || resource.role?(:Admin) 
            authenticated_root_url
        else
            authenticated_client_url
        end 
    end
end

在操作筛选器之前,可以在applicationcontroller中检查用户角色。我以前没有访问routes.rb中的当前用户。这种方法怎么样?这个对我有用。穆罕默德的建议也很有价值,但这个解决方案几乎不需要修改代码。1.从routes.rb中,我删除了从designe_scope开始的整个区块。。。2.我添加了:
design_for:users,controller:{sessions:'users/sessions',registrations:'users/registrations',passwords:'users/passwords',路径名:{sign_-in:'login',sign_-out:'logout'}
3。在/users/sessions_controller.rb中,我添加了:
def after_sign_in_path_for(resource)if resource.role.(:Architect)| resource.role.(:Admin)admin_dashboard_path else Client_dashboard_path end
@Michal如果用户手动输入url,您还需要在筛选之前添加。
root :to => 'pages#index', as: :unauthenticated_root
class SessionsController < Devise::SessionsController

    def after_sign_in_path_for(resource)
        if resource.role?(:Architect) || resource.role?(:Admin) 
            authenticated_root_url
        else
            authenticated_client_url
        end 
    end
end
devise_for :user, :controllers => {:sessions => "sessions"}