Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise after_sign_in_path_for ... sending to .... root_path - query

I need help with a routes issue with devise authentication gem to redirect to a custom page after successful login so as to create a new record by entering a test person name and age ( test data )

I am using Rails 3 with devise version 1.4.9

My routes are as below

    new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
        user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
       user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
   new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
  edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                     PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
   cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
   user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
   edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                     PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                     DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
             testers GET    /testers(.:format)             {:action=>"index", :controller=>"testers"}
                     POST   /testers(.:format)             {:action=>"create", :controller=>"testers"}
          new_tester GET    /testers/new(.:format)         {:action=>"new", :controller=>"testers"}
         edit_tester GET    /testers/:id/edit(.:format)    {:action=>"edit", :controller=>"testers"}
              tester GET    /testers/:id(.:format)         {:action=>"show", :controller=>"testers"}
                     PUT    /testers/:id(.:format)         {:action=>"update", :controller=>"testers"}
                     DELETE /testers/:id(.:format)         {:action=>"destroy", :controller=>"testers"}
                root        /                              {:controller=>"testers", :action=>"index"}

In applications controller i tried to override the method like below but to no avail i still get routed back to tester index

class ApplicationController < ActionController::Base

  protect_from_forgery

  def after_sign_in_path_for(resource) 

      new_tester_path

  end

end

In my routes.rb file i have the below lines

Testing::Application.routes.draw do

  devise_for :users 

  resources :testers

  root :to => 'testers#index'

While much of the code was done with scaffolding I was still not be able to figure how to redirect to new_tester_path or route /testers/new after successful sign_in by user email and password.

Can someone please let me know what i am missing..... while writing the override function, I would like to know the exact route i need to specify.

While testing i tried something stupid like this but the google page is also not opening ... :(

class ApplicationController < ActionController::Base 

protect_from_forgery 

helper ApplicationHelper

def after_sign_in_path_for(resource) 

"www.google.com" 

end 

def after_sign_up_path_for(resource) 

"www.google.com" 

end 

def after_update_path_for(resource) 

"www.google.com" 

end 
like image 709
Salman Ahmed Avatar asked Nov 22 '11 21:11

Salman Ahmed


2 Answers

Just use this snippet:

class ApplicationController < ActionController::Base
    def after_sign_in_path_for(user)
        user_url(user)
    end
end
like image 171
donnie rawlings Avatar answered Sep 22 '22 06:09

donnie rawlings


Try setting user_return_to path in session:

session['user_return_to'] = new_tester_path

You can do it in a controller derived from Devise::SessionsController

like image 43
cthulhu Avatar answered Sep 19 '22 06:09

cthulhu