I have two types of registration on on my website.
- User
- 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
- Name
- Password
- Phone Number
When a Host Signups he has to enter the following
- Name
- Password
- Phone Number
- 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