Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:58:10+00:00 2026-06-18T08:58:10+00:00

I use devise and tried to do the next thing: when the user sign

  • 0

I use devise and tried to do the next thing:

when the user sign in/up, I want to redirect him by his role_id (I let id of 1 for some users and 2 for the others).

if his role_id is 1, redirect him to tasksadmins_path, otherwise to workers_path.

so I tried something like:

routes.rb:

devise_for :users, :controllers => { :sessions => 'user_sessions'} do
   get '/users/sign_out' => 'devise/sessions#destroy'
   root to: "workers#index"
end

resources :tasksadmins

resources :workers

root to: "workers#index"

and this is my application_controller.rb:

class ApplicationController < ActionController::Base
    include ApplicationHelper

    protect_from_forgery
    before_filter :authenticate_user!

    rescue_from CanCan::AccessDenied do |exception|
        if current_user.role_ids == [2]
           redirect_to root_url
        else
           redirect_to tasksadmins_path
        end
    end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:58:11+00:00Added an answer on June 18, 2026 at 8:58 am

    the after_sign_in_path_for doesn’t work, so I added to ‘create’ the next lines:

    in the beginning, I wrote:

    resource = warden.authenticate!(:scope => resource_name)
    

    and then I wrote in the end of the ‘create’ function:

    sign_in(resource_name, resource)
    
    if current_user.role_ids == [2]
       respond_with resource, :location => workers_path
    else
       respond_with resource, :location => tasksadmins_path
    end
    

    so my create looks so:

    class UserSessionsController < Devise::SessionsController
        include ApplicationHelper
    
        def create
    
            resource = warden.authenticate!(:scope => resource_name)
    
            require "uri"
            require "net/http"
    
            ## get the user id from the database
            user_id = session['warden.user.user.key'][1][0];
    
            ## get the row by id
            user = User.find(user_id)
    
            # ============================
            # Ensure that a user exists
            # ============================
    
            code, body = http_request(Net::HTTP::Put.new("/api/v1/users/external/#{user_id}"), email: user.email);
            if code != 200
               Rails.logger.error("Unable to register user #{current_user.email} at Licensario");
            end
    
            sign_in(resource_name, resource)
    
            if current_user.role_ids == [2]
               respond_with resource, :location => workers_path
           else
               respond_with resource, :location => tasksadmins_path
           end
    
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
my plan is to use Devise to register a user and giving him the
I'm trying to customize my routes in devise. I've tried to use devise_scope :user,
Basically, I tried to use twitter-bootstrap-rails and devise gem in my app. However, when
I use Devise in Rails 3. I want to see name of current_user in
I currently use Devise 2.1 + Rails 3.2.x to authenticate users. I'm also going
I want to use devise's tokens to support logging in/out in multiple clients at
I'm using rails_admin which use devise. Some words still displays in english. I have
I have this application where I use Devise and, after login the user is
I would like to use the devise option :reconfirmable in my user model, so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.