Here is my user model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: { Buyer: 0, Seller: 0 }
end
Here is the routes .
Rails.application.routes.draw do
devise_for :users
root 'home#homepage'
get 'dashboard', to: 'home#dashboard'
end
Here is my my registration form
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div >
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div >
<%= 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 >
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<%= f.label :role %>
<%= f.select :role, User.roles.keys %>
<div >
<%= f.submit "Sign up", data: {turbo: false} %>
</div>
<% end %>
<%= render "devise/shared/links" %>
I don't know how to do it i trying so many method but it's not working for me. I want to do is when user select buyer it should go to the home page when user select seller it should go to the homepage
CodePudding user response:
First thing you need to do is create a registrations_controller
and customize the after_sign_up_path_for(resource
like so:
# app/controllers/users/registrations_controller.rb
module Users
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
# here you can check if the user is a buyer or seller and redirect to the correct path.
if current_user.buyer?
root_path
else
dashboard_path
end
end
end
end
After that you need to edit your routes so that devise will use this controller.
# app/config/routes.rb
devise_for :users, controllers: { registrations: 'users/registrations' }