[Ruby on Rails] How to use redirect_to

Introduction

After the process is finished (not finished) in the controller I have to think about which page to transition to, Use redirect_to at that time.

What is redirect_to

It is a method that can transition to the specified URL.

Basic writing


redirect_to "Redirect destination"

Actual code

We will update this code as soon as it is available.

Specified by URL


redirect_to "https://qiita.com/japwork"

Specify by path name


redirect_to root_path

Specified by action name


redirect_to action: :index

Specified by action name of different controller


redirect_to controller: :post, action: :index

Go back to previous page


redirect_to :back

Redirect the same page


redirect_to request.referer

Display a message on success

controller


redirect_to  root_path, notice: 'Success'

view


<%= notice %>

Difference from render

redirect_to executes an action and displays the view corresponding to that action.

render displays the view without taking any action. If the view has an instance variable to be acquired by the action, an error will occur.

Summary

By using it together with the if statement, the transition destination can be changed and displayed depending on the result. You need to know the various methods. In addition to the above, there is also a status code, so if you are interested, please check it out.

Also, on twitter, technologies and ideas that are not uploaded to Qiita are also uploaded, so I would be grateful if you could follow me. Click here for details https://twitter.com/japwork

Recommended Posts

[Ruby on Rails] How to use redirect_to
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to use session method
[Ruby on Rails] How to display error messages
[Rails] How to use enum
How to add / remove Ruby on Rails columns
How to use Ruby return
[Rails] How to use enum
How to use rails join
Ruby: How to use cookies
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
[Rails] How to use devise (Note)
How to deploy Bootstrap on Rails
How to use Bio-Formats on Ubuntu 20.04
[Ruby] How to use any? Method
Rails on Tiles (how to write)
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
How to display a graph in Ruby on Rails (LazyHighChart)
A memorandum on how to use Eclipse
How to use Apache Derby on Eclipse
How to use custom helpers in rails
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use MySQL in Rails tutorial
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to avoid creating unnecessary routes for devise
How to use Map
How to write Rails
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
Ruby on Rails Elementary