[Rails] Difference between redirect_to and render [Beginner]

Introduction

Suddenly, when I first saw redirect_to and render, I didn't really understand the difference between them. Let's dig deep and learn together!

Difference between redirect_to and render

redirect_to: Sends an HTTP request to the server and the user sees the HTML returned from it. render: A method that specifies the View file to call in the action.

I think that there are some parts that are difficult to understand with just words, so I will explain each ** processing flow **.

About the flow of each process

redirect_processing flow of to


① redirect in controller processing_Execute to
②redirect_HTTP request to the URL specified by the to argument(GET!!)Run
③ HTTP request(GET!!)Executes the routing process corresponding to the URL
(4) The controller and action corresponding to the routing process are called and the process is executed.
⑤ Render the view according to the process

① controller (redirect_to) → ② HTTP request (GET !!) → ③ routing → ④ controller → ⑤ view

Render process flow


① Execute render in the process of controller
(2) Display the view file specified by the render option

①controller(render) → ②view

It should be noted here that redirect_to is specified to be the route of ** GET method **.

When redirect_to and render are not specified

You see it once in a while. A controller like the one below.

controllers/books.rb


class BooksController < ApplicationController
  def index
  end
end

If nothing is mentioned in the action, render will be executed automatically! !! So in the above case, it's likely that the render'index'' has been omitted, and this action will call views / books / index.html.erb.

Frequently viewed description method

Now let's take a look at some commonly used examples of redirect_to and render!

controllers/books.rb


class BooksController < ApplicationController
  def create
    @book = Book.new(book_params)
    @book.user_id = current_user.id
    if @book.save
      redirect_to books_path, flash[:notice] = "Posting successful! !!"
    else
      flash.now[:alert] = "Posting failure!!"
      render 'new'
    end
  end
end

When posting, if the result of the save method is true, it redirects to the post list page, and if false, the page for new posts is redisplayed.

Why use render if false

Did you notice that you didn't call any action when viewing the view file using render? ↑ render'new' only displays books / new.html.erb, not via the new action.

In this pattern, I want to display a new page because posting failed, but if I use redirect_to, I call a new action and pass an empty method to the instance variable, so new The action will be reset and the data entered by the user will be erased. Therefore, the information for issuing the error message will also disappear, and the user will not be able to see "why could not post".

It's complicated, but it determines an important turning point, so I hope you'll face it until you're hungry.

(Supplement) About flash message at the time of render

I won't go into details because it's far from the main line, but if you want to send a message in the displayed view, write as follows.

redirect_In the case of to


flash[:notice] = "message"
Or
notice: "message"

For render


flash.now[:alert] = "message"  #now is on! !! !! !!

in conclusion

I explained the difference between redirect_to and render, which is easy for beginners to make mistakes! !! In the explanation, I often learn by myself, and I am keenly aware of the importance of output.

Recommended Posts

[Rails] Difference between redirect_to and render [Beginner]
[rails] Difference between redirect_to and render
Difference between redirect_to and render
Difference between redirect_to and render
Difference between render and redirect_to
Difference between render and redirect_to
Difference between render method and redirect_to
[Rails] I investigated the difference between redirect_to and render.
Difference between render and redirect_to, need for arguments
Rails: Difference between resources and resources
[Rails] Display error message-Differences between render and redirect_to, flash-
[Rails] Difference between find and find_by
Rails render redirect_to
[Rails] Differences between redirect_to and render methods and how to output render methods
Difference between vh and%
Difference between i ++ and ++ i
[Rails / ActiveRecord] About the difference between create and create!
Difference between member and collection of rails routes.rb
[Rails] Difference between create method and new + save method
[Rails] I learned about the difference between resources and resources
Difference between product and variant
[Java] Difference between == and equals
Difference between puts and print
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
[Java beginner] Difference between length and length () ~ I don't know ~
[Rails] What is the difference between bundle install and bundle update?
Difference between instance method and class method
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
Proper use of redirect_to and render
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Android] Difference between finish (); and return;
[Rails] Why is it render if save is successful and redirect_to fails?
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium