undefined method `user_url' for Devise SessionsController:create - ruby-on-rails

I have user model for authorization with devise gem. I want to add after_sign_in_path method:
# application_controller.rb
protected
# redirecting to appropriate url based on role
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
end
end
Whenever I try to sign in I get this error:
undefined method `user_url' for #<Devise::SessionsController:0x007fb89b5b00a8> Did you mean? course_url
I don't know why it says 'did you mean? course_url. But I have course model. And here are my routes:
authenticate :user do
resources :feeds, only: [:index]
resources :courses, only: [:index, :show]
# etc...
end
Also here is the code it points me:
if options.empty?
recipient.send(method, *args)
else
recipient.send(method, *args, options)
end
and first line of log:
actionpack (4.2.4) lib/action_dispatch/routing/polymorphic_routes.rb:220:in `polymorphic_method'
Whenever I commend after_sign_in_path_for I am able to sign in. If I comment contents of after_sign_in_path_for but leave empty after_sign_in_path_for method, I also get this error.
EDIT: I tested that I am not also signed in, not just not redirected. I think error happens right in the call after_sign_in_path_for, not in the redirect_to or whatever. Probably it has to do something with resource.
EDIT2: here are my rake routes:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
admin_root GET / rails_admin/main#dashboard
student_root GET / feeds#index
feeds GET /feeds(.:format) feeds#index
courses GET /courses(.:format) courses#index
course GET /courses/:id(.:format) courses#show
schools GET /schools(.:format) schools#index
school GET /schools/:id(.:format) schools#show
universities GET /universities(.:format) universities#index
university GET /universities/:id(.:format) universities#show
rails_admin /admin RailsAdmin::Engine
POST /graphql(.:format) graphql#create
landing_confirmation GET /landing/confirmation(.:format) landing#confirmation
landing_access_denied GET /landing/access_denied(.:format) landing#access_denied
root GET / landing#index
EDIT3: here is my github repo:
https://github.com/yerassyl/nurate

I had the same issue (Rails 7). I fixed it this way:
Add :turbo_stream as a navigational format. This line goes in config/initializers/devise.rb.
config.navigational_formats = ['*/*', :html, :turbo_stream]
Devise issue github

This error is thrown when the after_sign_in_path_for method is redefined in your app and returns nil value. As per my best guest, the provided snippet
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
end
end
is giving an error because none of the conditions are getting satisfied and hence the returned value is nil. To avoid such a case, you can always add an else condition which would get satisfied if no other does. Hence the below snippet should have worked (and will work for other users)
def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
else
some_other_path || root_path
end
end
Hope this helps someone. Cheers :)

In config/devise.rb include this line:
config.navigational_formats = ['/', :html, :turbo_stream]
before rails s
This has done the trick for me.

def after_sign_in_path_for(resource)
if current_user.has_role?(:admin)
dashboard_path
elsif current_user.has_role?(:student)
root_path
else
root_path
end
end
Try to add a else condition in your code. This worked for me. I missed something like this.

If you have the same error with Devise::RegistrationsController, the answers of Jishnu and antoniolulee also works fine.
You have to uncomment in config/initializers/devise.rb line says
config.navigational_formats = ['*/*', :html]
and add :turbo_stream like this
config.navigational_formats = ['*/*', :html, :turbo_stream]

Make sure you've got a method on your AplicationController like follows
def after_sign_in_path_for(resource)
resource.next_step
end
Next_step is an attribute stored in the record during its creation, you may decide to hard code here some other path.

Related

Passing nested params from create in redirect to show of nested controller

I have a schema like:
Company belongs to Users (devise)
Quote belongs to Company
Employees belong to Company
I have a simple_form_for using cocoon to create Company & Quote & Employees from the create action in the Company controller, all objects being created just fine by the create method. But on .save of these objects I am trying to redirect to Quotes#show of the quote created by Companies#create but I'm having trouble getting the quote_id over to Quotes#show.
Can you help me understand how to get the right params over to succesfully redirect? Thanks.
companies.rb
class CompaniesController < ApplicationController
before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
def new
#company = Company.new
#company.quotes.build
#company.employees.build
end
def create
#company = current_user.companies.new(company_params)
if #company.save
redirect_to company_quote_url(#company.id), notice: 'Quote request created'
else
render :new
end
end
private
def company_params
params.require(:company).permit(:co_name, :co_number, :postcode, :industry,
:quotes_attributes => [:id, :lives_overseas, :payment_frequency],
:employees_attributes => [:id, :first_name, :last_name, :email, :gender, :date_of_birth, :salary, :_destroy] )
end
end
quotes.rb
class QuotesController < ApplicationController
def show
#quote = #company.quotes.find(params)
end
end
routes
quotes_show GET /quotes/show(.:format) quotes#show
quotes_index GET /quotes/index(.:format) quotes#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
company_quotes GET /companies/:company_id/quotes(.:format) quotes#index
company_quote GET /companies/:company_id/quotes/:id(.:format) quotes#show
company_employees GET /companies/:company_id/employees(.:format) employees#index
company_employee GET /companies/:company_id/employees/:id(.:format) employees#show
companies GET /companies(.:format) companies#index
POST /companies(.:format) companies#create
new_company GET /companies/new(.:format) companies#new
edit_company GET /companies/:id/edit(.:format) companies#edit
company GET /companies/:id(.:format) companies#show
PATCH /companies/:id(.:format) companies#update
PUT /companies/:id(.:format) companies#update
DELETE /companies/:id(.:format) companies#destroy
root GET / companies#new
error message
No route matches {:action=>"show", :company_id=>65, :controller=>"quotes"} missing required keys: [:id]
I can't see how to get the quote_id route accross from Companies#create over to Quotes#show. When I run the redirect like; redirect_to company_quote_url(company_params) the error shows the header params being passed like this, i.e. this is what's available;
No route matches {:action=>"show", "co_name"=>"acme1", "co_number"=>"12345678", :controller=>"quotes",
"employees_attributes"=>{"0"=>{"first_name"=>"brian", "last_name"=>"blessed", "email"=>"brian#test.com", "gender"=>"m", "date_of_birth(1i)"=>"2001", "date_of_birth(2i)"=>"6", "date_of_birth(3i)"=>"28", "salary"=>"10000", "_destroy"=>"false"}}, "industry"=>"financial_services", "postcode"=>"al8 8ba",
"quotes_attributes"=>{"0"=>{"lives_overseas"=>"true", "payment_frequency"=>"annually"}}} missing required keys: [:company_id, :id]
I've played and failed with different variations of;
(params[:company][:quote_attributes[:id]])
(#company.quote.id)
yet i just can't seem to get it right, can anyone help please. Thanks
Since company has_many quotes, you should track the latest quote for that company and redirect to that show view of the quote.
def create
#company = current_user.companies.new(company_params)
if #company.save
#quote = #company.quotes.last
redirect_to company_quote_url(#company,#quote), notice: 'Quote request created'
else
render :new
end
end
last will give you the latest record with the help of created_at
Also you should tweak your quotes#show like below else it will error out.
def show
#company = Company.find(params[:company_id])
#quote = #company.quotes.find(params[:id])
end

Routes not changing upon editing controller

I'm configuring devise to work with two omniauth providers: twitter and github. Having implimented github first, I created a github method in my OmniauthCallbacksController. Deciding later that I wanted to use twitter as well, I reconfigured my controller, discarding the github method in favor of a more sophisticated implementation for both providers.
The Problem is that when I run rake routes, the old github method is all that appears and it seems that my changes to the controllers haven't even been recognized. I can even delete everything out of the controller and my rake routes result is left unchanged. I've attached the relevant files below. Many thanks in advance for any insight you can give me.
My routes file is below (note the existence of the now nonexistent users/omniauth_callbacks#github method):
Rails.application.routes.draw do
resources :issues
resources :submissions
resources :home
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
root to: "home#index"
end
When I run rake:routes, I get the following:
$ rake routes
Prefix Verb URI Pattern Controller#Action
issues GET /issues(.:format) issues#index
POST /issues(.:format) issues#create
new_issue GET /issues/new(.:format) issues#new
edit_issue GET /issues/:id/edit(.:format) issues#edit
issue GET /issues/:id(.:format) issues#show
PATCH /issues/:id(.:format) issues#update
PUT /issues/:id(.:format) issues#update
DELETE /issues/:id(.:format) issues#destroy
submissions GET /submissions(.:format) submissions#index
POST /submissions(.:format) submissions#create
new_submission GET /submissions/new(.:format) submissions#new
edit_submission GET /submissions/:id/edit(.:format) submissions#edit
submission GET /submissions/:id(.:format) submissions#show
PATCH /submissions/:id(.:format) submissions#update
PUT /submissions/:id(.:format) submissions#update
DELETE /submissions/:id(.:format) submissions#destroy
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
user_github_omniauth_authorize GET|POST /users/auth/github(.:format) users/omniauth_callbacks#passthru
user_github_omniauth_callback GET|POST /users/auth/github/callback(.:format) users/omniauth_callbacks#github
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
finish_signup GET|PATCH /users/:id/finish_signup(.:format) users#finish_signup
root GET / home#index
And my new Oauth callbacks controller looks like this:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
#user = User.find_for_oauth(env["omniauth.auth"], current_user)
if #user.persisted?
sign_in_and_redirect #user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
}
end
[:twitter, :github].each do |provider|
provides_callback_for provider
end
def after_sign_in_path_for(resource)
if resource.email_verified?
super resource
else
finish_signup_path(resource)
end
end
end
Figured it out! Turns out the issue wasn't with any of the files I posted. In the User model, I had the following line: omniauth_providers: [:github] I needed to add twitter to it.

Redirecting after sign in with Devise

I know this question has been asked quite a few times, but somehow it's not working for me. I would like the user to be redirected to a specific page after log in (using Devise). I'm using Rails 3.2 and Ruby 4.
As it is the user get redirected to:
- After login they get redirected to the root, if the user got to the login page from one of the pages controlled by the controller of which the root path is part of. These pages do not require authorization.
- After login they get redirected to the requested page, if the user automatically got to the login page by clicking on a page that requires authorization.
To redirect the user after login to a specific page, I understand that you need to add to the controller some lines. To the controller that is controlling the pages that required authorization I added:
class AccountsController < ApplicationController
before_filter :authenticate_user!
def user1_home
end
def user2_home
end
def user3_home
end
protected
def after_sign_in_path_for(resource)
'accounts/user1_home'
end
end
However, this didn't change anything. I also tried it with def after_sign_in_path_for(resource) 'accounts#user1_home_path' end
Should I perhaps also change something in routes.rb?
My routes are:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
toc GET /toc(.:format) pages#toc
stakeholder GET /stakeholder(.:format) pages#stakeholder
about GET /about(.:format) pages#about
doelgroep_onderneming GET /doelgroep_onderneming(.:format) pages#doelgroep_onderneming
doelgroep_ngo GET /doelgroep_ngo(.:format) pages#doelgroep_ngo
doelgroep_se GET /doelgroep_se(.:format) pages#doelgroep_se
partnership GET /partnership(.:format) pages#partnership
contact GET /contact(.:format) pages#contact
account_stakeholder GET /account/stakeholder(.:format) accounts#stakeholder_home
account_business GET /account/business(.:format) accounts#business_home
account_admin GET /account/admin(.:format) accounts#admin_home
root / pages#index
You definitely shouldn't be defining methods per user in the Accounts controller.
Devise has instructions on github for accomplishing this: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in
You need to move after_sign_in_path_for to the ApplicationController class and you shouldn't have 'accounts/user1_home'. To go to the right url for each use you should have something along the lines of user_path(#user) but I don't have enough of your code to know exactly what to use. You should run in a console window the rake routes command to see what you routes you have made available. Read more about routing at: http://guides.rubyonrails.org/routing.html
Yes. Set the following at any point (before or after login):
session["user_return_to"] = request.original_url # Will redirect the user to where they came from
If it's an authenticated page they tried to access before logging in you want to redirect to then you can simply do it this way in your ApplicationController:
def after_sign_in_path_for(resource)
if session[:user_return_to] == nil
your_actual_next_path
else
super
end
end

DEVISE after_inactive_sign_up_path_for not being called

Environment: RAILS 3.2 + DEVISE for auth + Invitable + Confirmable add-ons.
Using devise (2.2.3)
Using devise-i18n (0.6.5)
Using devise_invitable (1.0.3)
I am trying to redirect to a specific location after ACCEPT (TO SIGN UP), but only after_sign_in_path_for seems to be called after SIGN IN and ACCEPT.
I haven't been able to have after_accept_path_for working.
It continues to redirect to the "after sign in" location.
HERE THE CODE
In my routes.rb:
devise_for :users,
:controllers => { :registrations => 'registrations', :invitations => 'invitations' }
rake routes give me this:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
user_invitation POST /users/invitation(.:format) devise/invitations#create
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
PUT /users/invitation(.:format) devise/invitations#update
In my registration controller:
class RegistrationsController < Devise::RegistrationsController
# clear session
def create
super
session[:omniauth] = nil unless #user.new_record?
end
#protected
# after_sign_up_path_for doesn't seem to be called when using Confirmable module
# def after_inactive_sign_up_path_for(resource)
# #me_path
# session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
private
def build_resource(*args)
super
if session[:omniauth]
#user.apply_omniauth(session[:omniauth])
#user.valid?
end
end
end
Also
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for
session[:user_return_to].nil? ? me_path : session[:user_return_to]
end
end
In my application controller, (I left intentionally some commented code I tried to make it work):
def store_location
session[:user_return_to] = request.fullpath
end
# def after_sign_up_path_for
# me_path
# end
#
# def after_inactive_sign_up_path_for(resource)
# me_path
# #session[:user_return_to].nil? ? me_path : session[:user_return_to]
# end
# https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-(registration)
def after_sign_in_path_for(resource)
me_path
#dashboard_path
#session[:user_return_to].nil? ? dashboard_path : session[:user_return_to]
end
Any suggestions?
ADDED DEBUGGIN REDIRECTS
Add this to my application_controller
def redirect_to_with_logging(*args)
logger.debug "Redirect: #{args.inspect} from #{caller[0]}"
redirect_to_without_logging *args
end
alias_method_chain :redirect_to, :logging
After Sign in, works like a charm
Started POST "/users/sign_in" for 127.0.0.1 at 2013-04-25 14:20:04 +0200
Processing by Devise::SessionsController#create as HTML
[... I removed some of the Session creation info ...]
Redirect: ["/dashboard"] from /Users/joel/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.11/lib/action_controller/metal/responder.rb:135:in `redirect_to'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 968ms (ActiveRecord: 0.0ms)
DOCS:
After sign in
After sign up
After Accept <====
Override after_accept_path_for in Invitations controllers
class Users::InvitationsController < Devise::InvitationsController
protected
def after_accept_path_for(resource)
me_path
end
end
IMPORTANT:
put this file in 'controllers/users' directory
Fix the routes.rb to use the Users::InvitationsController
devise_for :users, :controllers => { :registrations => 'registrations', :invitations => 'users/invitations' }
You may put redirect_to my_specific_url at the end of sign_up controller's method, to redirect after signing up

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
Just use this snippet:
class ApplicationController < ActionController::Base
def after_sign_in_path_for(user)
user_url(user)
end
end
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
The Devise documentation explains all the steps to redirect to a specific page on successful sign in. By combine the techniques, you can redirect a user to many places after successful sign in.
Here is the resume:
You can do this in a controller you inherit from Devise::SessionsController - first, in controllers/users/sessions_controller.rb:
module Users
class SessionsController < Devise::SessionsController
def new
if params[:redirect_to].present?
self.resource = resource_class.new(sign_in_params)
store_location_for(resource, params[:redirect_to])
end
super
end
end
end
In config/routes.rb, you would have also added:
devise_for :users, controllers: {sessions: 'users/sessions'}
And you must add a custom after_sign_in_path_for in your ApplicationController
class ApplicationController < ActionController::Base
protected
def after_sign_in_path_for(resource)
stored_location_for(resource) || root_path
end
end
This works in all Devise versions, as I know.
I believe this is an inheritance issue. after_sign_in_path_for is originally defined within Devise::SessionsController. You can override it by making your SessionsController inherit from Devise::SessionsController, and then re-defining it within that controller.
If you are having issues trying to override the after_sign_in_path_for or after_sign_out_path_for helper methods within the ApplicationController of a Rails engine, you may want to check out this answer.
It describes how you'll need to override the SessionsController in your engine instead of the ApplicationController.
You don't seem to be doing anything wrong. Maybe this is a Devise issue.
Can you please try to isolate this on a Rails app and open an issue on Devise ?

Resources