[Rails] Differences between redirect_to and render methods and how to output render methods

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6.

redirect_to method

--redirect_to-> Routing-> Controller-> View. ――It's the same route as when a new request was sent. --The value of the original instance variable is __overwritten __.

redirect_to redirect path

render method

--_ Renderer → View __ moves in this order. --There are no new requests, __ as is, go straight to the view file __. --The instance variable is not overwritten because it does not go through the controller action. --Returns to the __view file, preserving the information you entered in the form.

render :Action name

Difference between redirect_to and render

__ Whether the information is updated through routing and controllers __.

Various ways to use the render method

Surprisingly, you can output various views with render.

Output action template for another controller

--You can do this with the render method. --You can output the view file by specifying the path starting from ʻapp / views` as shown below.

render "items/show"

Output any file

--You can output a file outside the application directory by using the absolute path.

render "/Users/k/projects/test/app/views/books/index.html.erb"

Output text

--The text is output as is with the : plain option. --Outputs an HTML string with the : html option.

render plain: "text"
render html: helpers.tag('Hello')

Reference site

https://railsguides.jp/layouts_and_rendering.html

Recommended Posts

[Rails] Differences between redirect_to and render methods and how to output render methods
[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
[Rails] Display error message-Differences between render and redirect_to, flash-
Differences between browser sessions and cookies and Rails session and cookies methods
[Rails] I investigated the difference between redirect_to and render.
Difference between render method and redirect_to
How to call classes and methods
Differences in how to handle strings between Java and Perl
[Introduction to Rails] How to use render
[Ruby on Rails] How to use redirect_to
How to use substring and substr methods
Rails render redirect_to
How to output Excel and PDF using Excella
Difference between render and redirect_to, need for arguments
How to execute and mock methods using JUnit
[Java] How to get and output standard input
How to build API with GraphQL and Rails
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Rails] How to get success and error messages
Rails scope anti-patterns and how to eliminate them
Ruby How to convert between uppercase and lowercase
How to access Java Private methods and fields
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
How to write Rails
How to uninstall Rails
[Rails] What is the difference between redirect and render?
[Rails] How to deal with URL changes after render
[Rails] How to edit and customize devise view and controller
(Ruby on Rails6) How to create models and tables
Let's sort out the differences between Java substring and C # Substring, and how to port them.
[rails] How to post images
How to prevent conflicts between JBoss modules and application libraries
Differences between namespace, module, scope and as in Rails routing
How to separate words in names in classes, methods, and variables
[Rails] How to use enum
Rails: Difference between resources and resources
Difference between Java and JavaScript (how to find the average)
[Forge] How to register your own Entity and Entity Render in 1.13.2
[Rails] How to install devise
Rails "render method" and "redirect method"
rails path and url methods
[Rails] How to define macros in Rspec and standardize processing
How to use rails join
How to set and describe environment variables using Rails zsh
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
Think about the differences between functions and methods (in Java)
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
[Ruby] How to get the value by specifying the key. Differences between hashes, symbols and fetch
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping