[Rails] How to get the URL of the transition source and redirect

Thing you want to do

I want to return (redirect) to the URL of the transition source after a specific process with the flea market app developed by Rails. Specifically ↓

  1. Press the purchase button from the product page
  2. If the credit card is not registered, ** Transition to the Creca registration screen **
  3. After registering your credit card, ** Return to the product page you were trying to purchase (redirect) **

Originally, after registering with Creca, the screen transitioned to the screen displaying card information. This was a very unfriendly behavior for users who had to find the product they were trying to buy again and press the buy button.

Original situation

Press the purchase button on the product page → Transition to the credit card registration screen (if the credit card is not registered) 92e9b1521895f97a6e1f2c4a46c7ce3f.gif

Credit card registration → Transition to the screen that displays the registered card information e94f61e09decdfded6051eabaa91cd4b.gif

This will result in poor usability, and the product you are trying to purchase may end up in a situation where you may find it difficult to find it again, so I'm looking for a solution. I've solved it well, so I'll describe it.

environment

Ruby 2.5.1 Rails 5.2.4

Use request.referer

The description itself is insanely easy.

creditcards_controller.rb


  def create
    render :new if params[:payjpToken].blank?
    customer = Payjp::Customer.create(card: params[:payjpToken])
    @card    = Card.new(user_id: current_user.id, payjp_id: customer.id)
    @card.save ? (redirect_to request.referer) : (render :new) #This line! !! !!
  end

In the line above end, I wrote redirect_to request.referer, but with this, I get the URL (HTTP referrer) before transitioning to the card registration screen (creditcards / new) and redirect it. ..

Revised

Card registration → Return to the page you were trying to purchase → Buy now! 480ebbde6bc9bd533df8fadda2e06681.gif

I was able to solve it like this.

(By the way, ? (): () Is a ternary operator. For details, see Mr. Ito's Qiita ([[For beginners] Idioms and useful methods that can be used for refactoring in Ruby and Rails](https: // qiita.com/jnchito/items/dedb3b889ab226933ccf))

Click here for Rails request. [[Rails Guide] 10 request and response objects](https://railsguides.jp/action_controller_overview.html#request%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7 % E3% 82% AF% E3% 83% 88% E3% 81% A8response% E3% 82% AA% E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3 % 83% 88)

I think you should google about referer (referrer) and HTTP referrer.

Recommended Posts

[Rails] How to get the URL of the transition source and redirect
[Java] How to get the URL of the transition source
Source used to get the redirect source URL in Java
[Rails] How to get the contents of strong parameters
[Rails] Get the path name of the URL before the transition and change the link destination
[Swift5] How to get an array and the complement of arrays
How to get today's day of the week
[Java] How to get the redirected final URL
[Rails] How to get success and error messages
[Java] How to get the authority of the folder
How to make the schema of the URL generated by Rails URL helper https
Get the URL of the HTTP redirect destination in Java
[Java] How to get the maximum value of HashMap
[Rails] How to change the column name of the table
[Android] How to get the setting language of the terminal
[Ruby] How to get the tens place and the ones place
Get the URL of the HTTP redirect destination in Ruby
[Swift] How to get the document ID of Firebase
How to embed and display youtube videos in Rails (You can also get the URL you entered by editing)
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
How to get the longest information from Twitter as of 12/12/2016
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
How to run React and Rails on the same server
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
[Rails] How to prevent screen transition
How to get along with Rails
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Swift5] How to analyze complex JSON and get the index of the element that satisfies the condition
How to mark up search keywords regardless of case and without changing the search source word
How to set the IP address and host name of CentOS8
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Rails] How to introduce kaminari with Slim and change the design
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Swift UI] How to get the startup status of the application [iOS]
How to get the contents of Map using for statement Memorandum
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
How to delete large amounts of data in Rails and concerns
Method to add the number of years and get the end of the month
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[Rails] Read the RSS of the site and return the contents to the front
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
[Docker] How to back up and restore the DB data of Rails application on docker-compose [MySQL]
[Rails] How to use the map method
How to determine the number of parallels
[Java] How to get the current directory
How to sort the List of SelectItem
How to find the tens and ones
How to get the date in java
The process of introducing Vuetify to Rails
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
[Java] How to get the current date and time and specify the display format
How to switch the display of the header menu for each transition page
How to change the maximum and maximum number of POST data in Spark
[Rails] How to get the user information currently logged in with devise
[Java] How to get to the front of a specific string using the String class
How to constrain the action of the transition destination when not logged in