简体   繁体   中英

STI with Rails and Devise

i'm working on a Ruby on Rails application and using devise to authenticate user.

First of all, i have these models:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable
end

class Admin < User
end

class Partner < User
end

My devise_for on routes:

devise_for :users, :controllers => {
  :registrations => "registrations",
  :passwords => "passwords"
}

What i want to do? I want do a separate devise for each model, with different views and different strategy, for example: The user login with his registration number and password, the admin and the partner login with email and password (i think that admin and partner devise can be the same, because thier login stratagy is the same).

What is the best way to do this?

Configure routes:

devise_for :users
devise_for :admins
devise_for :partners

Generate devise views

rails generate devise:views user
rails generate devise:views admin
rails generate devise:views partner

Don't forget, add type field in users table for your STI

class AddTypeToUser < ActiveRecord::Migration
  def up
    add_column :users, :type, :string
  end

  def down
    remove_column :users, :type
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM