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 ↓
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.
Press the purchase button on the product page → Transition to the credit card registration screen (if the credit card is not registered)
Credit card registration → Transition to the screen that displays the registered card information
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.
Ruby 2.5.1 Rails 5.2.4
The description itself is insanely easy.
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. ..
Card registration → Return to the page you were trying to purchase → Buy now!
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
[[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.