[Rails] What is the difference between redirect and render?

Introduction

Do you know what the difference between redirect and render is? If you are a Rails beginner, you may not understand it, so I will explain it briefly.

This is the difference

The difference is ** whether it goes through the controller **. I will write the flow of each process.

■ redirect Send request → Identify controllers and actions by routing → The action in the specified controller operates → The view file corresponding to the action that was performed is displayed as a response.

■ render Show specified view file

How to use it properly?

As an example that you often see, when you save the posted data, you can use redirect if the saving is successful and render if it fails. Let me explain a little with a code example.

post_controller


def new
  @post = Post.new
end

def create
  @post = Post.new(post_params)
  if @post.save
    redirect_to root_path
  else
    render "new"
  end
end

Let's see the actual movement in the video This time, we have validated it so that it cannot be saved unless you enter 5 or more characters.

81311fb1ec11688f69985b41a2984bc0.gif

You can easily understand that when the data is saved successfully, you just move to the root path (posted data list). If the save fails, render will read the new view file directly. At this time, since it does not go through the controller, the instance variable (@post) passed to the view file is not the one used in the new action, but the one used in the ** create action ** (post data is stored). By doing this, when the new view file is displayed after the save fails, it will ** retain the input data **. If you use redirect when saving fails, the new action will be loaded and @post will be passed a newly created empty instance, so the data you entered will be lost.

Finally

In addition to this usage, render is a very convenient method that can be used as a partial template or for iterative processing. If you can use it well, you will enjoy coding, so it's a good idea to remember it.

Thank you for watching until the end!

Recommended Posts

[Rails] What is the difference between redirect and render?
[Rails] What is the difference between bundle install and bundle update?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
[Rails] I investigated the difference between redirect_to and render.
What is the difference between Java EE and Jakarta EE?
[rails] Difference between redirect_to and render
What is the difference between an action and an instance method?
[Rails] Difference between redirect_to and render [Beginner]
What is the difference between a web server and an application server?
[Java] What is the difference between form, entity and dto? [Bean]
[Rails / ActiveRecord] About the difference between create and create!
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between redirect_to and render
Difference between render and redirect_to
Difference between render and redirect_to
Difference between render and redirect_to
[Rails] I learned about the difference between resources and resources
Difference between render method and redirect_to
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Jersey --What is Difference Between bind and bindAsContract in HK2?
About the difference between irb and pry
[Rails] I studied the difference between new method, save method, build method and create method.
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
Difference between render and redirect_to, need for arguments
Understand the difference between abstract classes and interfaces!
Difference between member and collection of rails routes.rb
[Rails] Difference between create method and new + save method
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Ruby] I thought about the difference between each_with_index and each.with_index
Understanding ruby's "|| =" specification and what is Rails presence method?
[Rails] Display error message-Differences between render and redirect_to, flash-
About the difference between classes and instances in Ruby
Compare the difference between dockerfile before and after docker-slim
[Swift] UITextField taught me the difference between nil and ""
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
Difference between product and variant
What is the pluck method?
[Java] Difference between == and equals
Difference between puts and print
What is the BufferedReader class?
Rails is difficult and painful!
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Rails is difficult and painful! Ⅱ
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
What is the constructor for?
Difference between StringBuilder and StringBuffer
What is Microservices? And Microservices Frameworks