Search code examples
ruby-on-railsdevise

How to signup in two different pages with devise


I used devise inthe login system.I want create two new html under the registrations, which is two different roles' pages. And I want to go to that page by choosing roles in registrations/new, which is the original signup file. What should I do next?

my files order in the view/user/registrations: enter image description here.

  • edit.html.erb
  • new.html.erb
  • new_librarian.html.erb
  • new_student.html.erb

I want to signup in the new two pages through registrations/new

<!--       what i changed in the registrations/new                        -->
<h2>Sign up</h2>

<p>Sign up as a Student<%= link_to 'Student', users_registrations_new_student_path, :method => 'get' %></p>
<p>Sign up as a Librarian<%= link_to 'Librarian', users_registrations_new_librarian_path, :method => 'get'%></p>

<!--       what is in the registrations/new_student               -->
<h2>Sign up</h2>



<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", adminv: resource %>

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

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

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

  <div class="field">
    <%= f.label :select_role %><br />
    <%= f.collection_select :role, User::ROLES, :to_s, :humanize %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>
<!--       what i changed in the routes.rb                        -->
  get 'users/registrations/new_librarian'
  get 'users/registrations/new_student'
Could not find devise mapping for path "/users/registrations/new_student". This may happen for two reasons: 
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]

Solution

  • You can achieve this by overriding the devise registration controller. In your overrided registration controller, in the new method once the user is saved,

    #app/config/routes.rb
    devise_for :users, :controllers => {:registrations => "registrations"}
    
    # app/controllers/registrations_controller.rb
    class RegistrationsController < Devise::RegistrationsController
    def new
      if @user.save!
        if @user.role == "librarian"
          redirect_to librarian_profile_path
        elsif @user.role == "student"
          redirect_to student_profile_path
         end
      end
    end
    end
    

    You can define librarian_profile_path and student_profile_path in your routes

    #app/config/routes.rb
     get 'librarian/new' => 'librarian_profile#new', :as => 'librarian_profile'
     get 'student/new' => 'student_profile#new', :as => 'student_profile'