2016-08-31 3 views
0

私は、ユーザーの電子メールとそのIPを収集するキャンペーンを作成するためのレールアプリケーションを用意しています。これが発生すると、情報はユーザーの作成に使用されます。deviseを持つユーザーとは異なるモデルを使用する方法

ただし、このキャンペーンを作成して管理するためのアカウントを作成して登録できる別のユーザーが必要です。私は1つだけのユーザーモデルを使用することに慣れているので、2つの方法を区別する方法が正確にはわかりません。

路線:以下

は、その現在の設定方法です

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 

スキーマ:

# 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 

は、より多くの情報が必要な場合、私に教えてください、私は喜んでそれを更新します。

答えて

2

をあなたは工夫モデルをいくつでも作成することができます。ただ、ヘルパー関数の名前は、あなたが

rails generate devise User 

を行うならば、ヘルパー関数は

before_action :authenticate_user! 
current_user 
user_signed_in? 

になりますように変更しかし、あなたは

rails generate devise Admin 

を行うならば、ヘルパー関数は次のようになります

before_action :authenticate_admin! 
current_admin 
admin_signed_in? 
+0

これは私がやりたい/やりたいことでした。そうでない場合は更新されます。正確なマーキング。 – Kevin

0

異なるユーザーモデルを持つ必要があります。キャンペーン、クライアント、および管理ユーザーを区別するためにユーザーモーダルで役割を持つことができます。

+0

確かに、私はその使用が分かりませんrモデルはDeviseによって使用されています。私が「レールを作ってsomemodelnameを作ろう」とすれば、うまくいくと思われる新しいモデルが得られることが分かりました。たぶん、誰かがこれが不正確で正しい練習かどうかを知っているでしょうか? – Kevin

+0

はい、rails generate devise MODELを使用して、deviseを持つ新しいユーザーモードを作成できます。あなたのコードでは 'admin_users'が有効になっています。 – pankaj

0

両方のモデルで機能する必要がある場合は、二重のデバイスが必要なようです。通常のUserAdminUserは全く異なるので、モデル上でSTI(単一テーブル継承)またはプレーンrole属性を実際に使用することはできません。基本的には、両方のアカウントに同時にログインすることができます。あなただけの、その後、さまざまな工夫モデルを持っているドキュメントを参照する必要がある場合は

http://devise.plataformatec.com.br/#configuring-models

は単に

rails generate devise AdminUser

を実行し、それはあなたのために仕事を行います。 (例えばuser_sessionとして、あまりにも他の方法で動作します - >admin_user_session)の代わりにcurrent_userのコントローラの使用current_admin_userに現在のユーザーのインスタンスを取得するには

関連する問題