根据在rails中使用用户角色登录,重定向到特定主页

时间:2015-10-28 18:24:28

标签: ruby ruby-on-rails-4 devise rolify

首先,我是Ruby和Ruby on Rails的新手,他们只与他们合作了几天。我目前正在尝试重定向用户根据其角色登录特定页面后登陆的页面,我不太清楚如何操作。有谁知道我怎么会接近这个?我使用了以下链接但无法使用它们。

我使用Devise进行身份验证,并使用Rolify作为我的角色。

这是我的routes.rb文件:

    Rails.application.routes.draw do
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
    devise_for :admins
    devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}
    devise_scope :user do
      authenticated :user do
        root :to => 'pages#videolibrary'
      end

  unauthenticated :user do
    root :to => 'devise/registrations#new', as: :unauthenticated_root
  end
end

我已经制作了一个直通控制器,但我不知道如何连接控制器以便它被使用。或者即使这是解决这个问题的正确方法。

这是passthroughcontroller.rb:

class PassthroughController < ApplicationController
  def index
    if current_user.has_role? :admin
      redirect_to 'pages#videolibrary'
    elsif current_user.has_role? :retail
      redirect_to 'pages#retail'
    elsif current_user.has_role? :commercial
      redirect_to 'pages#commercial'
    elsif current_user.has_role? :business
      redirect_to 'pages#business'
    elsif current_user.has_role? :manager
      redirect_to 'pages#videolibrary'
    else
      redirect_to 'devise/registrations#new'
    end
  end
end

我已经尝试过关注这些帖子,但却无法让它发挥作用:

Devise Redirects to specific page based on User Role on Login

How can I redirect a user's home (root) path based on their role using Devise?

how to change the rails root url based on the current user or role

使用此链接,我的lib目录中没有role_constraint.rb文件。

也许这里有人知道如何处理这个问题?

修改

我已将我的应用程序控制器更新为现在阅读:

class ApplicationController < ActionController::Base
      # Prevent CSRF attacks by raising an exception.
      # For APIs, you may want to use :null_session instead.
      protect_from_forgery with: :exception

        before_action :configure_permitted_parameters, if: :devise_controller?
      protected
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_up)  { |u| u.permit(  :email, :password, :password_confirmation, role: []  ) }
      end

    def after_sign_in_path_for(resource)
      # Here you can write logic based on roles to return different after sign in paths
      if current_user.has_role? :admin
        get 'pages/videolibrary' => "pages#videolibrary", as: :retail_page
      elsif current_user.has_role? :retail
        get 'pages/retail' => "pages#commercial", as: :retail_page
      elsif current_user.has_role? :commercial
        get 'pages/commercial' => "pages#commercial", as: :commercial_page
      elsif current_user.has_role? :business
        get 'pages/business' => "pages#business", as: :business_page
      elsif current_user.has_role? :manager
        get 'pages/manager' => "pages#manager", as: :manager_page
      else
        get 'devise/registrations' => "devise/registrations#new"
      end
    end
end

这是我的routes.rb:

Rails.application.routes.draw do
  mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
  devise_for :admins
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}
devise_scope :user do
  authenticated :user do
    root :to => 'pages#videolibrary'
  end
  unauthenticated :user do
    root :to => 'devise/registrations#new', as: :unauthenticated_root
  end
    get 'pages/welcome'
    get 'pages/home' => "pages#videolibrary"
    get 'videolibrary' => "pages#videolibrary"
    get 'pages/*page' => "pages#show"
    get 'pages/retail' => "pages#retail"
    get 'pages/commercial' => "pages#commercial"
    get 'pages/business' => "pages#business"
    get 'pages/manager' => "pages#manager"
  end
end

如果需要,这是我的user.rb文件:

class User < ActiveRecord::Base
  rolify
  resourcify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  ROLES = %w[admin retail commercial business manager]

end

这些是带有佣金路线的令人信服的路线:

                 root GET    /                               pages#videolibrary
 unauthenticated_root GET    /                               devise/registrations#new
        pages_welcome GET    /pages/welcome(.:format)        pages#welcome
           pages_home GET    /pages/home(.:format)           pages#home
         videolibrary GET    /videolibrary(.:format)         pages#videolibrary
                      GET    /pages/*page(.:format)          pages#show
          retail_page GET    /pages/retail(.:format)         pages#retail
      commercial_page GET    /pages/commercial(.:format)     pages#commercial
        business_page GET    /pages/business(.:format)       pages#business
         manager_page GET    /pages/manager(.:format)        pages#manager

我现在在Devise中获取NoMethodError :: SessionsController #create

未定义的方法`get&#39; for #Devise :: SessionsController:0x9879660

    get 'pages/manager' => "pages#manager", as: :manager_page
  else
    get 'devise/registrations' => "devise/registrations#new"
  end
end

1 个答案:

答案 0 :(得分:1)

Devise有一个可以覆盖的控制器方法,名为after_sign_in_path_for,这在设计文档here中有所讨论。 (devise wiki,特别是如何举例说明非常有用)

在您的应用程序控制器中

def after_sign_in_path_for(resource)
  # Here you can write logic based on roles to return different after sign in paths
  if current_user.has_role? :admin
    videolibrary_path
  elsif current_user.has_role? :retail
    retail_page_path
  elsif current_user.has_role? :commercial
    commercial_page_path
  elsif current_user.has_role? :business
    business_page_path
  elsif current_user.has_role? :manager
    videolibrary_path
  else
    new_user_path
  end
end

您需要添加页面的路由,例如

get 'pages/retail => "pages#retail", as: :retail_page # this sets up a named link 'retail_page_path' which you can use in controllers and views

查看rails routing guide了解详情。此外,在编写路线后,您可以在控制台中运行rake routes以输出所有应用路线及其指定路线(如retail_page_path