0

I have two types of registration on on my website.

  1. User
  2. Host

There are two models related to Registration

users(id, name, is_host, ...)

hosts(company_name, user_id, status, ...)

Every Host is a User by default on the application. When the User signups on the website he has to enter the following fields

  1. Name
  2. Email
  3. Password
  4. Phone Number

When a Host Signups he has to enter the following

  1. Name
  2. Email
  3. Password
  4. Phone Number
  5. Company Name

On submitting the form as a host it should save the data in the User model and also set the is_host flag to 1 which other wise is 0 and then store the company_name in the hosts model.

What i have done?

registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  def new_host
    build_resource({})
    #set_minimum_password_length
    #yield resource if block_given?
    #respond_with self.resource
  end

  def create_host
    # build_resource(sign_up_params)
    # resource_saved = resource.save
    # yield resource if block_given?
    # if resource_saved
    #   if resource.active_for_authentication?
    #     set_flash_message :notice, :signed_up if is_flashing_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_flashing_format?
    #     expire_data_after_sign_in!
    #     respond_with resource, location: after_inactive_sign_up_path_for(resource)
    #   end
    # else
    #   clean_up_passwords resource
    #   set_minimum_password_length
    #   respond_with resource
    # end

    puts "********************************************"
  end

  def update
    # For Rails 4
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
    # For Rails 3
    # account_update_params = params[:user]

    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

  def update_password
    @user = User.find(current_user.id)
    if @user.update_with_password(account_update_params)
      # Sign in the user by passing validation in case their password changed
      # sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end

  protected

  def after_update_path_for(resource)
    edit_user_registration_path
  end

end

new_host.html.rb

<h2>Sign up!!</h2>

<%= form_for(resource, as: resource_name, url: simaple_path) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <% f.label :password_confirmation %><br />
    <% f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "users/shared/links" %>

routes.rb

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

  devise_scope :user do
    match '/become_a_host' => 'registrations#new_host', :via => :get
    match '/create_host' => 'registrations#new_host', :via => :post, as: :simaple
    #get 'registrations#create_host', :via => :post
  end
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • Please look at this http://stackoverflow.com/questions/7299618/multiple-user-models-with-ruby-on-rails-and-devise-to-have-separate-registration – djadam Dec 18 '14 at 13:20
  • i checked that link. they use multiple models. here my model is one. all i want to do is change the a column is_host = 1 in the user model which i can also use for authorization. – Harsha M V Dec 18 '14 at 14:13

1 Answers1

0

You can keep the default "devise_for :users" and have the default devise functionality AND have a second one that is located at /hosts/users/.

routes.rb

devise_for :users

namespace :host do
  devise_for :users do
    controller :registrations do
      get 'new' => :new_host
      post 'update' => :update
      post 'create' => :create_host
    end
  end
end

Just combine this with your custom RegistrationsController and modify the registration flow for hosts only.

Georgi Atsev
  • 2,775
  • 2
  • 16
  • 18