[RUBY] [Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in

The title is long. Here is a summary of friendly forwarding.

What is Friendly Forwarding?

It's a kind of process like "The page you requested, the page after login, but you haven't logged in yet. I'll guide you to the page you wanted to go to after logging in." Kind design that you often see. It's fine.

How do you make it?

The general flow is as follows.

  1. Temporarily save the requested URL
  2. Skip to the login screen and confirm your login
  3. Revert to the requested URL

Method definition

Prepare a method (1) for temporarily saving the request and a method (3) for returning to the temporarily saved URL. This time, in sessions_helper.rb, write as follows.


# 1.Temporarily save the requested URL
def store_location
  session[:forwarding_url] = request.original_url if request.get?

# 3.Revert to the requested URL
def redirect_back_or(default)
  redirect_to(session[:forwarding_url] || default)

Save the requested URL (request.original_url) as session [: forwarding_url] in 1. ʻIf request.get?` limits the request type to GET actions only.

3 is the process to return to the temporarily saved URL or to return to the URL specified as the default. Even if the URL does not exist||After that, the URL set in default can be entered. Also, if you keep the session information, it will be read and strange at the next login, so the URL temporarily saved with session.delete (: forwarding_url) on the second line in the method Delete.

Temporarily save the requested URL + login confirmation

Call store_location to temporarily store the URL you are trying to access. Here, I will write it together with the description to check if the logged-in user is below private of ʻusers_controller.rb`.


class UsersController < ApplicationController
  before_action :user_login_required



def user_login_required
  unless logged_in? #When not logged in
    store_location #Temporarily save the URL here!
    flash[:danger] = "Please login" #Show flash message
    redirect_to login_url #Forced to send to login page

Revert to the requested URL

Use redirect_back_or (default) to specify the transition destination after login. It is OK if you write the following in the place where the login process is performed.


def create
  user = User.find_by(email: params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    log_in user
    params[:session][:remember_me] == '1' ? remember(user) : forget(user)
    flash[:success] = "You are now logged"
    #Specify the redirect destination below. default uses user here, but user_Can be written like url
    redirect_back_or user 
    flash.now[:danger] = 'I failed to login'
    render 'new'

This completes the implementation! If you dare to log in after deporting from an existing URL, you will be taken to that URL. Thank you for your support.

at the end

As mentioned above, I tried to reorganize each role in my own way so that it is easy to understand at a glance. I hope you find it helpful.

I referred to the following for implementation. Thank you very much.

-10.2.3 Friendly Forwarding (Rails Tutorial) -The most suitable technical blog in the world --Friendly forwarding

Recommended Posts

[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
[rails devise] How to transition users who are not logged in to the login page
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Rails] How to get the URL of the transition source and redirect
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
Incident (rails) that is logged out when user editing of the application
[Rails] How to get the user information currently logged in with devise
How to constrain the action of the transition destination when not logged in
[Rails] How to solve the time lag of created_at after save method
How to get the ID of a user authenticated with Firebase in Swift
How to make a unique combination of data in the rails intermediate table
[Ruby On Rails] How to search and save the data of the parent table from the child table
Determine that the value is a multiple of 〇 in Ruby
How to prevent users who are not logged in from transitioning to the new or edit page
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
How to delete large amounts of data in Rails and concerns
[Rails] Read the RSS of the site and return the contents to the front
How to identify the path that is easy to make a mistake
How to write the view when Vue is introduced in Rails?
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
A memo that was soberly addicted to the request of multipart / form-data
How to interact with a server that does not crash the app
How to test a private method in Java and partially mock that method
The story of forgetting to close a file in Java and failing
To you who lament that the conversion of JODConverter + LibreOffice is slow
Confirmation and refactoring of the flow from request to controller in [httpclient]
[Rails] About the error that the image is not displayed in the production environment
How to update user edits in Rails Devise without entering a password
How to change the maximum and maximum number of POST data in Spark
[Rails] Use devise to get information about the currently logged in user
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
[Rails] How to create a table, add a column, and change the column type
[Rails] The cause of not being able to post was in form_with
How to make the schema of the URL generated by Rails URL helper https
Change the save destination of the image to S3 in the Rails app. Part 2
Ubuntu 18 is the OS that adds a NIC to a server instance in Sakura's cloud and assigns a local IP.
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
What to do about "A server is already running ..." that happened without turning off the rails server in the terminal
How to insert a video in Rails
A solution to an error that makes you angry that you are not following the MySQL default setting ONLY_FULL_GROUP_BY in a production environment and it is not unique.
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile
How to resolve errors that occur in the "Ruby on Rails" integration test
[Error] How to resolve the event that the screen does not transition after editing
[Rails] How to get rid of flash messages in a certain amount of time
How to set when "The constructor Empty () is not visible" occurs in junit
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
How to deploy VS Code Remote Containers in a docker-compose project that includes both the API and the front app
Convert to a tag to URL string in Rails
[Rails] Button to return to the top of the page
How to prevent direct URL typing in Rails
How to implement a like feature in Rails
How to easily create a pull-down in Rails