簡體   English   中英

設計中的多個模型

[英]Multiple Models in Devise

我正在嘗試在Devise中創建多個模型(使用Ruby 2.0,Rails 4)。 我搜索過Stack Overflow並嘗試使用Google,但大多數人最終都使用了繼承或CanCan。

問題是我需要兩個完全不同的模型才能以兩種完全不同的方式注冊和使用我的網站 - 一種使用電子郵件或用戶名登錄,並具有各種管理功能(例如查看MobileUser的圖表)用法等)。 另一個只能從移動設備訪問,使用電話號碼登錄,並且具有更簡單的界面。

我試圖為兩者運行Devise('rails g devise WebUser'和'rails g devise MobileUser')。 我現在有兩個表和列,但只有WebUser的路由和模型。 我嘗試手動為MobileUser添加路由和模型,但沒有任何作用 - 模型沒有連接到表,路由產生運行時錯誤('WebUser不響應'設計'方法)。

我讀過的所有內容都表明我可以讓Devise在我的一個網站上為兩個模型工作 - 我該如何實現?

我的路線:

Mailer::Application.routes.draw do
  devise_for :web_users
  # devise_for :mobile_users (doesn't work, so it is commented out)

  resources 'web_users', only: [:show, :index]
  resources 'mobile_users', only: [:show, :index]

  root 'static_pages#home'

  match '/contact',  to: 'static_pages#contact',  via: 'get'
  match '/faq',      to: 'static_pages#faq',      via: 'get'
end

以下摘錄來自設計寶石自述文件:

使用以下命令生成模型:

rails generate devise MODEL

使用以下命令定義自定義視圖:

rails generate devise:views MODELS

使用以下命令生成自定義控制器

rails generate devise:controllers [scope]

然后在你的路線

devise_for :MODELS, controllers: { sessions: "MODELS/sessions" }

我一直在使用多個模型的設計使用這些步驟沒有任何困難...

我知道這是一個非常古老的問題,但也許它會幫助2019年的Devise支持devise_group的人

Thins使用多個Devise模型非常容易。 我有三種不同的設計模型,如下所示:

User.rb

class User < ApplicationRecord
    has_many :messages

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
end

Organizer.rb

class Organizer < ApplicationRecord
    has_one :organizer_profile, dependent: :destroy, autosave: true, inverse_of: :organizer
    has_many :brochures, dependent: :destroy
    has_many :messages, as: :messageable

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable
end

Sponsor.rb

class Sponsor < ApplicationRecord
    has_one :sponsor_profile, dependent: :destroy, autosave: true, inverse_of: :sponsor
    has_many :sponsorings
    has_many :brochures, through: :sponsorings

    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

    scope :approved, -> { where(approved: true) }

    # Allow saving of attributes on associated records through the parent,
    # :autosave option is automatically enabled on every association
    accepts_nested_attributes_for :sponsor_profile
end

的routes.rb

Rails.application.routes.draw do
    # Deviseable resources
    devise_for :organizers, controllers: {registrations: "organizers/registrations"}
    devise_for :sponsors, controllers: {registrations: "sponsors/registrations"}
    devise_for :users

    root "welcome#index"
end

現在我不能使用相同的authenticate_user! user_signed_in? 甚至是current_user 那么現在,我怎么知道是否有用戶登錄? 我們可能需要做像user_signed_in? || organizer_signed_in? || sponsor_signed_in?這樣的事情user_signed_in? || organizer_signed_in? || sponsor_signed_in? user_signed_in? || organizer_signed_in? || sponsor_signed_in? 但這太重復了代碼。

感謝devise_group選項,我們可以簡化此工作流程!

在你的application_controller.rb添加:

class ApplicationController < ActionController::Base
    # Devise working with multiple models.
    # Define authentication filters and accessor helpers for a group of mappings.
    devise_group :member, contains: [:organizer, :sponsor, :user]

    private

    def pundit_user
        # Make Pundit to use whichever Devise model [Organizer, Sponsor, User] as the 'current_user'
        # Just by using the offered helper method from Devise, 'devise_group' feature
        current_member
    end
end

現在您可以使用通用幫助程序authenticate_member! member_signed_in? 甚至current_member在需要時驗證任何Devise用戶。 或者您仍然可以像往常一樣為每種類型的Devise用戶分別使用幫助程序。

希望它對某人有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM