How to return to the previous page after the Devise pointer

After successfully logging in through Devise, how can I redirect the user to the page they were on?

I already read and searched a lot, and I understand that I need to define after_sign_in_path_for . I did this and it works correctly, what I came across, understands how the previous page is stored, and how to properly name it.

I have this in the session controller:

 def after_sign_in_path_for(resource) return request.env['omniauth.origin'] || session[:user_return_to] || root_path end 

I also tried

 ... return request.env['omniauth.origin'] || stored_location_for(resource) || root_path ... 

I don’t think I understand how to save the location, since the user is redirected back to the root path if he clicks to enter.

A sign can be initiated in two ways. Either (a) the user tries to access the restricted view (i.e. before_filter :authenticate_user!... , in which case they are redirected and requested to enter the system. Or (b) the user clicks the link for the character that is available on each page if the user is not logged in.

(a) seems to work. (b) no. I think I need to save the current location to the session when the user clicks the login link.

How can I do it? Or, where is a good source of information that will help me figure this out.

Thanks!

+4
source share
4 answers

You can get the previous url using request.referrer , as explained in this SO question: How to redirect to the previous page in Ruby On Rails?

+9
source

Use

 redirect_to request.referrer 
+4
source

Heads up: the code should go in your application_controller.rb, not in the session controller.

How it works is that newer versions of Devise automatically save the start page that the user tried to visit (before redirecting it to enter).

It can be accessed using the helper: stored_location_for(resource)

What you add to the application controller looks something like this:

 def after_sign_in_path_for(resource) stored_location_for(resource) || users_dashboard_path # replace users_dashboard_path by whichever route you want to redirect to after login - default is root_path end 

After a successful login, Devise will automatically run your method from the application controller instead of its own (overriding it).

For a more complex example, here is one from the Devise team:

 def after_sign_in_path_for(resource) stored_location_for(resource) || if resource.is_a?(User) && resource.can_publish? publisher_url else super end end 

I hope this answer helps anyone who is looking for how to do this with newer versions of development.

+1
source

Here is another approach. I enabled this "return to" function only on certain links (via the continue URL parameter). In the example below, I check t params[:continue] before assigning it session[:continue] to after_action , although perhaps this is not necessary if you have authorization in place. Finally, in the after_sign_in_path_for method after_sign_in_path_for redefine delete session[:continue] , meanwhile using it (as it is not needed later) - deleting the / value key returns the value if the key matches, otherwise nil returned, in which case the operator returns to root_path

app / views / log / show.html.erb

 <%= link_to 'sign in', new_user_session_path(continue: journal_url(@journal)) %> or <%= link_to 'sign up', new_user_registration_path(continue: journal_url(@journal)) %> 

application / controllers / application_controller.rb

 after_action :store_location def store_location if params[:continue] =~ /\/(journal\/[0-9]+|foo|bar)\z/ # safelist session[:continue] = params[:continue] end end def after_sign_in_path_for(resource) session.delete(:continue) || root_path end 
0
source

Source: https://habr.com/ru/post/1412915/


All Articles