Setting up different User models and registration paths for Devise on Ruby on Rails

↘锁芯ラ 提交于 2019-12-04 19:55:47

I'd go with one User model and a two stage signup. First they would click on their desired button, each one passing a unique 'role' param in the URL and going to the devise signup page. Here they would enter only their email/password and we would pass the param from the URL to a simple 'role' hidden field in the form.

Then as step 2, after technically registering, they are directed to a separate edit account type page (each user having a different account, outlined below) to fill in the rest of their details.

The models:

models/user.rb

class User < ActiveRecord::Base
  has_one :account
  has_one :business_account
  has_one :manager_account
end

models/account.rb

class Account
  belongs_to :user

models/business_account.rb

class BusinessAccount
  belongs_to :user

models/manager_account.rb

class ManagerAccount
  belongs_to :user

Then, using devise, I'd override the registrations_controller to add a role based on a hidden field in the first step simple registration form (which would just be email/password/role).

In that file, I'd also override the after_signup_path method, to redirect to an edit_account type page for the relevant account we create for them during signup.

First the routes:

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

resources :users do 
  resource :account
  resource :business_account
  resource :manager_account
end

Then the controller (see comments within code):

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

The above would redirect them to a separate accounts controller/view depending on the account type. This solution would save you a lot of headaches down the line.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!