How to Use a different model than Users with devise

时间:2016-08-31 17:31:43

标签: ruby-on-rails ruby devise

I have a rails application that creates a campaign that collects users emails and their IP. When this occurs, the information is used to create a User.

However I need another user that can sign up and create an account to create and manage this campaign. I'm not exactly sure how to differentiate between the two as I am used to using only one User model.

Below is the way its currently set up:

Routes:

Name::Application.routes.draw do

  ActiveAdmin.routes(self)

  devise_for :admin_users, ActiveAdmin::Devise.config

  root :to => "users#new"

  post 'users/create' => 'users#create'
  get 'refer-a-friend' => 'users#refer'
  get 'privacy-policy' => 'users#policy'

  unless Rails.application.config.consider_all_requests_local
    get '*not_found', to: 'users#redirect', :format => false
  end
end

Schema:

# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130312045541) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "active_admin_comments", force: :cascade do |t|
    t.string   "resource_id",   null: false
    t.string   "resource_type", null: false
    t.integer  "author_id"
    t.string   "author_type"
    t.text     "body"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.string   "namespace"
  end

  add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree
  add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree
  add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree

  create_table "admin_users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "admin_users", ["email"], name: "index_admin_users_on_email", unique: true, using: :btree
  add_index "admin_users", ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true, using: :btree

  create_table "delayed_jobs", force: :cascade do |t|
    t.integer  "priority",   default: 0
    t.integer  "attempts",   default: 0
    t.text     "handler"
    t.text     "last_error"
    t.datetime "run_at"
    t.datetime "locked_at"
    t.datetime "failed_at"
    t.string   "locked_by"
    t.string   "queue"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

  add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree

  create_table "ip_addresses", force: :cascade do |t|
    t.string   "address"
    t.integer  "count"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email"
    t.string   "referral_code"
    t.integer  "referrer_id"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

end

Let me know if more information is needed and I'll gladly update it.

3 个答案:

答案 0 :(得分:2)

You can create any number of devise models. Just the names of helper functions change like if you do

rails generate devise User

then helper functions will be

before_action :authenticate_user!
current_user
user_signed_in?

But if you do

rails generate devise Admin

then helper functions will be

before_action :authenticate_admin!
current_admin
admin_signed_in?

答案 1 :(得分:0)

There is on need to have different user model, you can have role in user modal to differentiate campaign, client and admin user.

答案 2 :(得分:0)

If you need to have a functionality of devise in both models - it looks like you need a double devise. Since your regular User and AdminUser are totally different you can not really use STI (single table inheritance) or plain role attribute on the model. Basically, double devise means you can login into both accounts even simultaneously.

If you just need to have a different devise model, then refer to the docs: http://devise.plataformatec.com.br/#configuring-models

Simply run

rails generate devise AdminUser

and it will do the job for you. To get the instance of current user in controller use current_admin_user instead of current_user (works with other methods too, such as user_session -> admin_user_session)