Search code examples
ruby-on-railsrubydeviseruby-on-rails-5

devise Register one users sign up, but need two different register paths


As i been work for this project for while, as this one was not on the scopes, until yesterday, as the clients and project manager change mind as they want two different signup as it was one single sign up before but now want change two, but i was thinking it possible use same one user, but different path

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>

  </div>
  <div class="col-sm-offset-4 col-sm-4 margin-button-bottom">
    <div class="col-sm-12 text center">
      <div class="inner-addon right-addon">
        <i class="custom custom-icon-arrow-right"></i>
        <%= link_to "I want hire Equipment", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %>
      </div>
    </div>
  </div>

  <div class="col-sm-offset-4 col-sm-4  margin-button-bottom">
    <div class="col-sm-12 text center">
      <div class="inner-addon right-addon">
        <i class="custom custom-icon-arrow-right"></i>
        <p><%= link_to "I represent a business with equipment available for hire", new_user_registration_path, class: "btn btn-black-free-account btn-lg btn-block", role:"button" %></p>
      </div>
    </div>
  </div>
  <br/>

so if this button "I represent a business with equipment available for hire" show form same as user but add field is Company

Here is two different

devise register new.html.erb

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>
    <br />

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= render 'shared/regmessage' %>

    <div class="form-group">
      <%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
    </div>

    <div class="form-group">
      <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
    </div>

    <div class="form-group">
      <% if @minimum_password_length %>
      <em>(
        <%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
    </div>

    <div class="actions">
      <%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
    </div>
    <% end %>

    <hr />
    <%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
      <span class="fa fa-facebook"></span> Sign in with Facebook
    <% end %>
    <hr />
    <%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
      <span class="fa fa-google"></span> Sign in with Google
    <% end %>
  <br/>
  </div>
</div>

same as above but add is company field in the new_company.html.erb

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <h2 class="text-center">Sign up</h2>
    <br />

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= render 'shared/regmessage' %>

    <div class="form-group">
      <%= f.text_field :company, autofocus: true, placeholder: "Company Name", class: "form-control", autocomplete: "Company-name" %>
    </div>

    <div class="form-group">
      <%= f.text_field :fullname, autofocus: true, placeholder: "Full name", class: "form-control", autocomplete: "fullname" %>
    </div>


    <div class="form-group">
      <%= f.email_field :email, autofocus: true, placeholder: "Email", class: "form-control", autocomplete: "email" %>
    </div>

    <div class="form-group">
      <% if @minimum_password_length %>
      <em>(
        <%= @minimum_password_length %> characters minimum)</em>
      <% end %><br />
      <%= f.password_field :password, placeholder: "Password", class: "form-control", autocomplete: "new-password" %>
    </div>

    <div class="actions">
      <%= f.submit "Sign up", class: "btn btn-black-free-account btn-block" %>
    </div>
    <% end %>

    <br/>
    <%= link_to user_facebook_omniauth_authorize_path, class: "btn btn-block btn-social btn-facebook" do %>
      <span class="fa fa-facebook"></span> Sign in with Facebook
    <% end %>
    <br/>
    <%= link_to user_google_oauth2_omniauth_authorize_path, class: "btn btn-block btn-social btn-google" do %>
      <span class="fa fa-google"></span> Sign in with Google
    <% end %>
  <br/>


  </div>
</div>

Route.rb

  devise_for  :users,
              path: '',
              path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
              :controllers => {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations', }

register controller.rb

class RegistrationsController < Devise::RegistrationsController
  protected
    def update_resource(resource, params)
      resource.update_without_password(params)
    end
end

I was thinking something like java-script or controller if i click business button then the route will know and add company field into new.html.erb rather decided to go which one of two new and new company.html.erb

maybe new ID in the new.html.erb or need configure at route or controller?


Solution

  • In your registrations controller set a flag variable @for_company for example based on the params[:for_company] presence:

    class RegistrationsController < Devise::RegistrationsController
      def new
        @for_company = params[:for_company].present?
        super
      end
    end
    

    And in your Sign Up page simply add for_company: true param to a link to a company sign up:

    <%= link_to "I represent a business with equipment available for hire", new_user_registration_path(for_company: true), class: "..." %>
    

    Then just show :company field in case if @for_company is true.