[英]Multiple user models with Ruby On Rails and devise to have separate registration but one common login
[英]Setting up different User models and registration paths for Devise on Ruby on Rails
我對紅寶石很陌生,幾個月來我一直在為此苦苦掙扎。 我進行了廣泛的搜索,嘗試了答案的答案,但還是沒有運氣。 (我在Ruby On Rails上嘗試了多個用戶模型,並設計了單獨的注冊路徑,但是有一個通用的登錄路徑,但是沒有用)
我目前有一個user.rb模型,它可以連接設計並可以正常工作。
1-在注冊頁面上,我想有3個按鈕,這些按鈕將導致產生單獨的注冊表格(每個用於企業,經理和已經存在的用戶)。 我是否要在routes.rb中進行設置? 2-表單將具有不同的屬性,這些屬性將填充各自的數據庫。 3-完成表格后,他們將被定向到各自的路線。 用戶使用當前的默認路由,而企業使用的是業務儀表板,而經理使用的是經理儀表板。 是在routes.rb中還是設計出來?
我將不勝感激任何指導!
我已經閱讀了有關devise,cancan和rolify的文檔,但似乎無法將所有內容結合起來為我工作。
我對紅寶石很陌生,幾個月來我一直在為此苦苦掙扎。 我進行了廣泛的搜索,嘗試了答案的答案,但還是沒有運氣。 (我在Ruby On Rails上嘗試了多個用戶模型,並設計了單獨的注冊路徑,但是有一個通用的登錄路徑,但是沒有用)
我目前有一個user.rb模型,它可以連接設計並可以正常工作。
1-在注冊頁面上,我想有3個按鈕,這些按鈕將導致產生單獨的注冊表格(每個用於企業,經理和已經存在的用戶)。 我是否要在routes.rb中進行設置? 2-表單將具有不同的屬性,這些屬性將填充各自的數據庫。 3-完成表格后,他們將被定向到各自的路線。 用戶使用當前的默認路由,而企業使用的是業務儀表板,而經理使用的是經理儀表板。 是在routes.rb中還是設計出來?
我將不勝感激任何指導!
我已經閱讀了有關devise,cancan和rolify的文檔,但似乎無法將所有內容結合起來為我工作。
#user.rb
class User < ActiveRecord::Base
has_many :contibutions
rolify
# Include default devise modules. Others available are:
# :lockable, :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
def admin?
has_role?(:admin)
end
def self.find_for_oauth(auth, signed_in_resource = nil)
# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)
user = identity.user
if user.nil?
# Get the existing user from email if the OAuth provider gives us an email
user = User.where(:email => auth.info.email).first if auth.info.email
# Create the user if it is a new registration
if user.nil?
user = User.new(
name: auth.extra.raw_info.name,
#username: auth.info.nickname || auth.uid,
email: auth.info.email.blank? ? TEMP_EMAIL : auth.info.email,
password: Devise.friendly_token[0,20]
)
user.skip_confirmation!
user.save!
end
# Associate the identity with the user if not already
if identity.user != user
identity.user = user
identity.save!
end
end
user
end
end
我將使用一個用戶模型和兩個階段的注冊。 首先,他們將單擊所需的按鈕,每個按鈕都會在URL中傳遞一個唯一的“角色”參數,然后轉到設備注冊頁面。 在這里,他們只輸入電子郵件/密碼,我們會將參數從URL傳遞到表單中的簡單“角色”隱藏字段。
然后,在步驟2中,經過技術注冊后,他們將被定向到一個單獨的編輯帳戶類型頁面(每個用戶都有一個不同的帳戶,如下所述),以填寫其其余詳細信息。
型號:
型號/user.rb
class User < ActiveRecord::Base
has_one :account
has_one :business_account
has_one :manager_account
end
型號/帳戶.rb
class Account
belongs_to :user
型號/business_account.rb
class BusinessAccount
belongs_to :user
型號/manager_account.rb
class ManagerAccount
belongs_to :user
然后,使用devise,我將覆蓋registrations_controller以在第一步簡單注冊表單(這只是電子郵件/密碼/角色)中基於隱藏字段添加角色。
在該文件中,我還將覆蓋after_signup_path方法,以重定向到我們在注冊期間為他們創建的相關帳戶的edit_account類型頁面。
首先路線:
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
resource :account
resource :business_account
resource :manager_account
end
然后是控制器(請參閱代碼中的注釋):
controllers / registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
if resource.save
# you will name the following param. make sure it's in devise strong_params
# also the == will depend on how you pass the role - string, integer etc
if sign_up_params[:role] == "1"
user.add_role :standard
resource.build_account(user_id: resource.id) # code to create user account
elsif sign_up_params[:role] == "2"
user.add_role :manager
resource.build_manager_account(user_id: resource.id) # code to create user account
elsif sign_up_params[:role] == "2"
user.add_role :business
resource.build_business_account(user_id: resource.id) # code to create user account
end
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
protected
# override the after signup path to your desired route, e.g
def after_sign_up_path_for(resource)
if sign_up_params[:role] == "1"
edit_user_account_path(resource.id)
elsif sign_up_params[:role] == "2"
edit_user_manager_account_path(resource.id)
elsif sign_up_params[:role] == "2"
edit_user_business_account_path(resource.id)
end
end
end
上面將根據帳戶類型將它們重定向到單獨的帳戶控制器/視圖。 此解決方案將為您省去很多麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.