[Rails] I investigated the difference between redirect_to and render.

The redirect and render are lined up in the create and update actions of the Rails controller, but I didn't understand how they differed, so I looked it up. I didn't really care when I was just starting to learn, but the other day I was asked the difference between the two and couldn't answer well. .. ..

Conclusion

【render】 Call the specified view directly from the controller. Movement: controller → view Replace only the screen without changing the state of data etc.

【redirect_to】 Same process as when receiving an http request Movement: controller → URL → route → controller → view Access the specified URL from scratch and get all the data again

I actually tried it

① Let's see the movement with the create action of the normal controller file

tasks_controller.rb


 def create
   @task = Task.new(task_params)
   if @task.save
     redirect_to tasks_path, notice: "Created a task"
   else
     render :new
   end
 end 

スクリーンショット 2020-12-28 21.20.47.png Leave the task name blank so that it will not be saved due to validation. Now when you press "Create Task" ... スクリーンショット 2020-12-28 21.20.47.png It was not saved and "render: new" was executed. The entered data remains the same.

(2) I replaced render with redirect. Specifies that Prefix calls new.html.erb as it does for render.

tasks_controller.rb


def create
  @task = Task.new(task_params)
  if @task.save
    redirect_to tasks_path, notice: "Created a task"
  else
    redirect_to new_task_path #render render redirect_Replaced with to
  end
end

スクリーンショット 2020-12-28 21.20.47.png Again, save the task name in the blank on purpose. When you press "Create Task" ... スクリーンショット 2020-12-28 20.21.00.png Everything I entered is blank. This time there is no problem because the number of characters is small in the test, but if an error occurs when trying to post a blog with a large number of characters, the written sentence will be a bubble of water.

A Rails guide when you're in trouble about what's happening.

Rails Guide Description

Below is a description of the Rails guide for render and redirect_to.

Rails Guide Layout and Rendering 2.3 Use redirect_to ( https://railsguides.jp/layouts_and_rendering.html#redirect-to%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B)

render is for specifying which view (or other asset) to use when configuring the response. The redirect_to method is fundamentally different from the render method in this respect. The redirect_to method tells the browser to resend the request to another URL.

Rails Guide Layout and Rendering 2.3.2 Differences between render and redirect_to After executing> redirect_to, the code finishes executing there and waits for the next request from the browser (normal standby state). Immediately after that, the browser sends a request to another URL to the server according to the HTTP status code 302 sent to the browser with redirect_to, and the server processes the request again. I'm not doing anything else.

It's a little confusing. .. ..

I also referred to this article very much, and now I understand what is happening. Difference between render and redirect

The render retains the input contents and directly calls the specified view to replace the screen. It seems that redirect_to is re-requesting the http request and recalling the specified view through route and controller from scratch.

By the way, I tried another one.

③ Try changing redirect_to to render I set it to move to the index action when the task is saved successfully.

tasks_controller.rb


def create
  @task = Task.new(task_params)
  if @task.save
    render :index
  else
    render :new
  end
end

Enter all items including the task name so that the task will be saved this time. スクリーンショット 2020-12-28 20.51.52.png

Now press Create Task. .. .. スクリーンショット 2020-12-28 15.45.40.png An error has occurred. This is probably because her instance variable @task in index.html.erb is empty. @task is generated by the controller's index action. In other words, you are trying to call the specified view, but the instance variable has not been created because it does not go through the index action of the controller. Therefore, it seems that an error has occurred.

On the other hand, if you try using redirect_to.

tasks_controller.rb


def create
  @task = Task.new(task_params)
  if @task.save
    redirect_to tasks_path
  else
    render :new
  end
end

Enter all items so that they are saved. スクリーンショット 2020-12-28 20.51.52.png Press Create Task. .. スクリーンショット 2020-12-28 23.14.38.png The task was saved and moved to the index view.

Recommended Posts

[Rails] I investigated the difference between redirect_to and render.
[rails] Difference between redirect_to and render
[Rails] Difference between redirect_to and render [Beginner]
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 and redirect_to
Difference between render method and redirect_to
[Rails] I learned about the difference between resources and resources
[Rails] What is the difference between redirect and render?
Difference between i ++ and ++ i
[Rails / ActiveRecord] About the difference between create and create!
Rails: Difference between resources and resources
[Rails] I studied the difference between new method, save method, build method and create method.
[Rails] Display error message-Differences between render and redirect_to, flash-
[Rails] Difference between find and find_by
[Rails] What is the difference between bundle install and bundle update?
[Rails] Differences between redirect_to and render methods and how to output render methods
Rails render redirect_to
About the difference between irb and pry
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
[iOS] Understand the difference between frame and bounds
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Difference between member and collection of rails routes.rb
[Rails] Difference between create method and new + save method
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
About the difference between classes and instances in Ruby
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
[Swift] UITextField taught me the difference between nil and ""
[Java beginner] Difference between length and length () ~ I don't know ~
Difference between product and variant
I investigated the enclosing instance.
[Java] Difference between == and equals
Difference between puts and print
Rails "render method" and "redirect method"
Difference between CUI and GUI
Difference between variables and instance variables
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
Difference between StringBuilder and StringBuffer
Difference between Java and JavaScript (how to find the average)
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
What is the difference between an action and an instance method?
[Java] Check the difference between orElse and orElseGet with IntStream
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
I will explain the difference between Android application development and iOS application development from the perspective of iOS engineers
Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.
[Ruby] Difference between get and post
Find the difference between List types
Difference between interface and abstract class