{"id":10700,"date":"2024-06-20T06:02:37","date_gmt":"2024-06-20T06:02:37","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10700"},"modified":"2024-06-20T06:02:37","modified_gmt":"2024-06-20T06:02:37","slug":"redirect-admin-and-user-in-rails","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/redirect-admin-and-user-in-rails","title":{"rendered":"Redirection Admin and User in Rails"},"content":{"rendered":"<p>Here&#8217;s a guide to set up redirection for admins and regular users to different home pages after login in a Rails application:<\/p>\n<h3>1. Override after_sign_in_path_for<\/h3>\n<p>Override the after_sign_in_path_for method in your ApplicationController:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass ApplicationController &lt; ActionController::Base\r\n  protect_from_forgery with: :exception\r\n\r\n  def after_sign_in_path_for(resource)\r\n    if resource.admin?\r\n      rails_admin_path  # Redirect admin to RailsAdmin dashboard\r\n    else\r\n      user_home_path    # Redirect regular users to user home page\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>2. Define Routes<\/h3>\n<p>Ensure routes are defined in config\/routes.rb:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nRails.application.routes.draw do\r\n  mount RailsAdmin::Engine => '\/admin', as: 'rails_admin'\r\n\r\n  get 'user_home', to: 'users#home', as: 'user_home'\r\n\r\n  devise_for :users\r\n\r\n  root 'home#index'\r\nend\r\n<\/pre>\n<h3>3. Create User Home Action<\/h3>\n<p>Add a home action to UsersController:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass UsersController < ApplicationController\r\n  before_action :authenticate_user!\r\n\r\n  def home\r\n  end\r\nend\r\n<\/pre>\n<h3>4. Create User Home View<\/h3>\n<p>Create the view for the user home page:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n<!-- app\/views\/users\/home.html.erb -->\r\n<h1>Welcome to your home page, <%= current_user.email %>!<\/h1>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a guide to set up redirection for admins and regular users to different home pages after login in a Rails application: 1. Override after_sign_in_path_for Override the after_sign_in_path_for method in your ApplicationController: class ApplicationController &lt; ActionController::Base protect_from_forgery with: :exception def after_sign_in_path_for(resource) if resource.admin? rails_admin_path # Redirect admin to RailsAdmin dashboard else user_home_path # Redirect regular [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10701,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-10700","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10700"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10700"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10700\/revisions"}],"predecessor-version":[{"id":10703,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10700\/revisions\/10703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10701"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}