简体   繁体   中英

How to use two different custom signup forms with two different models with devise and rails?

I have two different models professional and employer. Both need a specific sign_up or registration page. They have some things in common but many more that are not so single table. I am also using devise. Here are the models:

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

validates_presence_of :name, :email, :phone_number, :address, :city, :state, :zip, :skills, 
   :experience, :languages, :software, :education, :pay_range, :bio
validates :phone_number, format: /\(?(\d{3})\)?[\W\D]?(\d{3})[\W\D]?(\d{4})/
validates :email, format: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/

has_many :jobs
has_one :resume
has_many :reviews, as: :reviewable
has_many :contracts
has_many :employers, through: :contracts
has_one :user, :as => :rolable

end

and the other one:

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

  validates_presence_of :name, :email, :phone_number, :address, :city, :state, :zip, 
      :office_type, :number_of_hygenists, :assisted?, 
      :software_type, :paperless?, :parking, :patients_per_day, :languages_required, 
      :about
  validates :phone_number, format: /\(?(\d{3})\)?[\W\D]?(\d{3})[\W\D]?(\d{4})/
  validates :email, format: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/

  has_many :jobs
  has_many :reviews, as: :reviewable
  has_many :professionals, through: :contracts
  has_many :contracts
  has_one :user, :as => :rolable

 end

The problem is that when I try to make the view go to the signup of either signup view for either model it just goes though the devise controller.

Here is the root page:

   %h1 Staffolution
   %br
   %br
   %h2 This is the placeholder for the welcome/home page
   %br
   %br
   .container.c
     %p 
       For Employers
       %ul.nav
         %li 
           = link_to 'Sign In',  new_employer_session_path
         %li
           = link_to 'Sign Up',  new_employer_registration_path
     %p
       For Profesionals     
       %ul.nav
         %li 
           = link_to 'Sign In',  new_professional_session_path
         %li
           = link_to 'Sign Up',  new_professional_registration_path

Here is the routes.rb:

 Rails.application.routes.draw do
   devise_for :professionals
   devise_for :employers

   mount JasmineRails::Engine => '/specs' if defined?(JasmineRails)

   root to: 'pages#home'

   resources :professionals 
   resources :employers
 end

Here is my rake routes: (sorry for the bad format)

                      Prefix Verb   URI Pattern                            Controller#Action
    new_professional_session GET    /professionals/sign_in(.:format)       devise/sessions#new
        professional_session POST   /professionals/sign_in(.:format)       devise/sessions#create
destroy_professional_session DELETE /professionals/sign_out(.:format)     devise/sessions#destroy
       professional_password POST   /professionals/password(.:format)     devise/passwords#create
   new_professional_password GET    /professionals/password/new(.:format)  devise/passwords#new
  edit_professional_password GET    /professionals/password/edit(.:format) devise/passwords#edit
                             PATCH  /professionals/password(.:format)     devise/passwords#update
                             PUT    /professionals/password(.:format)     devise/passwords#update
 cancel_professional_registration GET /professionals/cancel(.:format) devise/registrations#cancel
   professional_registration POST   /professionals(.:format)   devise/registrations#create
  new_professional_registration GET    /professionals/sign_up(.:format)  devise/registrations#new
   edit_professional_registration GET    /professionals/edit(.:format)  devise/registrations#edit
                             PATCH  /professionals(.:format)  devise/registrations#update
                             PUT    /professionals(.:format)    devise/registrations#update
                             DELETE /professionals(.:format)  devise/registrations#destroy
        new_employer_session GET    /employers/sign_in(.:format)           devise/sessions#new
            employer_session POST   /employers/sign_in(.:format)           devise/sessions#create
    destroy_employer_session DELETE /employers/sign_out(.:format)  devise/sessions#destroy
           employer_password POST   /employers/password(.:format)   devise/passwords#create
       new_employer_password GET    /employers/password/new(.:format)      devise/passwords#new
      edit_employer_password GET    /employers/password/edit(.:format)     devise/passwords#edit
                             PATCH  /employers/password(.:format)  devise/passwords#update
                             PUT    /employers/password(.:format)  devise/passwords#update
   cancel_employer_registration GET    /employers/cancel(.:format)   devise/registrations#cancel
       employer_registration POST   /employers(.:format)   devise/registrations#create
   new_employer_registration GET    /employers/sign_up(.:format)  devise/registrations#new
  edit_employer_registration GET    /employers/edit(.:format)    devise/registrations#edit
                             PATCH  /employers(.:format)   devise/registrations#update
                             PUT    /employers(.:format)       devise/registrations#update
                             DELETE /employers(.:format)          devise/registrations#destroy
               jasmine_rails        /specs                                 JasmineRails::Engine
                        root GET    /                                      pages#home
               professionals GET    /professionals(.:format)               professionals#index
                             POST   /professionals(.:format)               professionals#create
            new_professional GET    /professionals/new(.:format)           professionals#new
           edit_professional GET    /professionals/:id/edit(.:format)      professionals#edit
                professional GET    /professionals/:id(.:format)           professionals#show
                             PATCH  /professionals/:id(.:format)           professionals#update
                             PUT    /professionals/:id(.:format)           professionals#update
                             DELETE /professionals/:id(.:format)           professionals#destroy
                   employers GET    /employers(.:format)                   employers#index
                             POST   /employers(.:format)                   employers#create
                new_employer GET    /employers/new(.:format)               employers#new
               edit_employer GET    /employers/:id/edit(.:format)          employers#edit
                    employer GET    /employers/:id(.:format)               employers#show
                             PATCH  /employers/:id(.:format)               employers#update
                             PUT    /employers/:id(.:format)               employers#update
                             DELETE /employers/:id(.:format)               employers#destroy

   Routes for JasmineRails::Engine:
    root GET  /           jasmine_rails/spec_runner#index

Here are the controllers:

   class EmployersController < ApplicationController

     before_action :authenticate_employer!

     def index

     end

     def new
       @employer = Employer.new
     end  

     def edit
       @employer = current_employer
     end

     def update
       @employer = Employer.find(params[:id])
       @employer.update_attributes(employer_params)
       redirect_to employers_path 
     end

     def employer_params
       params.require(:employer).permit(:name, :phone_number, :address, 
         :office_type, :number_of_hygenists, :assisted?, :software_type, 
         :paperless?, :patients_per_day, :parking, :languages_required, 
         :about, :city, :state, :zip)
     end

and:

   class ProfessionalsController < ApplicationController

     before_action :authenticate_professional!

     def new
       @professional = Professional.new params[:professional]
       if @event.save!   
         redirect_to professional_index_path
       else
     end  

     def edit
       @professional = current_professional
     end

     def update
       @professional = Professional.find(params[:id])
       @professional.update_attributes(professional_params)
       redirect_to professional_path 
     end

     def employer_params
       params.require(:professional).permit(:name, :email, 
         :phone_number, :address, :city, :state, :zip, :skills, 
         :experience, :languages, :software, :education, 
         :pay_range, :bio)
     end
   end

Any help would be great I am kinda new and kinda not but mostly new to rails.

Your problem does not seem clear to me. Although I am guessing you want to have custom controller for devise. You need to create a new controller like say registration controller and override the devise registration controller

class RegistrationsController < Devise::RegistrationsController
  def create
    #Insert Your Logic here
  end
end

Also you would have to add the below in the routes and add the paths as per your need.

 devise_for :users, :controllers => { :registrations => "registrations" }, :path => "", :path_names => {:sign_out => "signout", :sign_up => "signup"}

Hope it helps.

EDIT :

You can also have different register actions for the different roles. You could define professional_user_register method for professional users and employee_register for employees. You could have to define these actions in the routes. See below samples

Routes

devise_scope :user do
    post "professional/register" =>"registrations#employee_register"
end

devise_scope :user do
    post "employee/register" =>"registrations#professional_user_register"
end

Registration Controller

def employee_register
  #Employee Register Code
end

def professional_user_register
  #Professonal User Register Code
end

Devise allows you to set up as many Devise models as you want.

Configuring multiple models

You can configure views for each model

Configuring views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM