多个设计用户类型根路由

时间:2014-04-22 15:24:58

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

我刚刚使用Devise User模型设置了我的STI结构。我有两种用户类型(业务和贷方),并希望在登录后将它们发送到单独的根路径。我的模型如下所示:

user.rb
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :type
end

lender.rb
Class Lender < User
end

business.rb
Class Business < User
end 

我的路线文件如下:

Lendingloop::Application.routes.draw do
  # User route
  devise_for :users, skip: [:registrations]

  # Lending routes
  devise_for :lenders, skip: :sessions
  authenticated :lender do
    root :to => "LenderAccount#dashboard", :as => "lender_authenticated_root"
  end 

  #Business routes
  devise_for :businesses, skip: :sessions
  authenticated :business do
    root :to => "BusinessAccount#dashboard", :as => "business_authenticated_root"
  end 

  # Error Routes
  get "/404", to: 'errors#not_found'
  get "/422", to: 'errors#unacceptable'
  get "/500", to: 'errors#internal_error'

  # Root route
  root :to => 'StaticPages#landing'
end

正如您所看到的,我有一个经过身份验证的do块,我想将贷方和企业重定向到他们的特定根页。我有一个LenderAccount和BusinessAccount控制器设置,其中包含仪表板操作和相应的视图。

当我作为贷方或企业登录时,我被重定向到我的root_path,即'StaticPages#landing'。这应该只适用于未登录的用户。

更新

我浏览了设计文档并将以下内容添加到我的application_controller.rb文件中:

  def after_sign_in_path_for(user)
    if user.type == "Business"
      business_authenticated_root_path
    elsif user.type == "Lender"
      lender_authenticated_root_path
    else 
      root_path
    end 
  end 

当我尝试直接从lender_authenticated_root_path转到仪表板页面时,我的控制台中也发现了一个奇怪的错误:

Rendered /Users/questifer/.rvm/gems/ruby-1.9.2-p320@rails3tutorial/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)
[2014-04-22 12:31:31] ERROR Errno::ECONNRESET: Connection reset by peer
    /Users/bvlaar/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
    /Users/bvlaar/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
    /Users/bvlaar/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

有没有人知道如何让这些经过身份验证的块来处理正确的根重定向?

2 个答案:

答案 0 :(得分:3)

我通过将routes.rb文件更改为:

来完成工作

<强>的routes.rb

root :to => 'business_account#dashboard', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Business' }, :as => "business_root"
root :to => 'lender_account#dashboard', :constraints => lambda { |request| request.env['warden'].user.class.name == 'Lender' }, :as => "lender_root"

现在我的两种用户类型可以登录他们的帐户并被定向到他们各自的控制器和仪表板。

答案 1 :(得分:0)

我想我可以提供帮助。我遇到了类似的问题。在我的routes.rb文件中,我创建了一个&#34; home&#34;未登录用户的屏幕。

Rails.application.routes.draw do

  root "home#index"

  devise_for :users 

  resources :home
  resources :dashboard

home_controller.rb如下所示:

class HomeController < ApplicationController

def index
  if user_signed_in?
    redirect_to :controller=>'dashboard', :action =>'index'
  end
end

end

dashboard_controller.rb看起来像这样,仅适用于已登录的用户:

class DashboardController < ApplicationController
before_filter :authenticate_user!

def index    
end


end

每个视图(home.html.erb&amp; dashboard.html.erb)可以反映访问者或登录用户的内容。

我用这个教程来解决它。我不知道它是否有点过分,这是我的第一个申请,但似乎是你所要求的。

http://www.tonylea.com/2012/rails-authentication-with-devise/

希望有所帮助。