Rails "render method" and "redirect method"

Deep digging, memo

Difference between the two

· Call render and ** return to browser ** create complete ** response ** -Call redirect_to and send ** HTTP redirect code status ** to ** browser **

Summarize briefly render method ... Specify "** view file " to be displayed redirect method ... Specify " URL (HTTP request method) **"

"Redner" method

As explained above, the View ** you want to display in ** Action The method to specify

In that Action, what is stored as @ ~ (instance variable) is It is possible to call from View with Ruby syntax

View formats that can be called include ".html" and ".html.erb". You can also output text format, JSON, XML

Movement

Simple movement of ** controller → view ** Specify the view file you want to display, I'm just displaying it

"Redirect_to" method

To "resend" the request again to another URL Method for issuing commands to the browser

Send an HTTP request to the server The user will see the HTML returned from it

As a side note, in Rails' redirect_to method ** HTTP method is fixed to "GET" **

So ** Cannot redirect ** with POST request etc. **

Movement

controller → URL → route → controller → view

And you can see that it works with a completely different structure from the render method

Points to use properly, precautions

・ If you fail to log in or enter = ** I just want to display an error ** → render method -When data update or deletion is required = ** Controller processing ** is required → redirect_to method

・ Only one view can be returned from one Action

rb/app/controller/○○controller.rb


class ○○Controller < ApplicationController
  def create
    ~~
    if ~~
      ~~
      render "index"
    end
    render "new" ☓
    redirect_to "new" ○
  end
end

Reference article

https://railsguides.jp/layouts_and_rendering.html https://qiita.com/1ulce/items/282cccba1e44158489c8 https://qiita.com/kanpe777/items/c5154b58c852855deefc https://easyramble.com/cannot-post-redirect-on-rails.html https://stackoverflow.com/questions/985596/redirect-to-using-post-in-rails

Recommended Posts

Rails "render method" and "redirect method"
[Rails] require method and permit method
[Rails] What is the difference between redirect and render?
rails method
Difference between render method and redirect_to
[rails] Difference between redirect_to and render
Rails render redirect_to
Rails delegate method
variable and method
[Rails] Difference between redirect_to and render [Beginner]
[Rails] Difference between create method and new + save method
Rails valid? And invalid?
[Rails] devise helper method
About redirect and forward
[Rails] devise introduction method
Understanding ruby's "|| =" specification and what is Rails presence method?
[Rails] Display error message-Differences between render and redirect_to, flash-
[Rails] I investigated the difference between redirect_to and render.
Difference between redirect_to and render
about the where method (rails)
[Rails] N + 1 problems and countermeasures
Rails: Difference between resources and resources
Rails Posts and User Linkage
Difference between redirect_to and render
Rails Tutorial Records and Memorandum # 0
rails path and url methods
Rails is difficult and painful!
[Rails] Differences between redirect_to and render methods and how to output render methods
Introducing Bootstrap and Font-Awesome (Rails)
Rails is difficult and painful! Ⅱ
Java methods and method overloads
Difference between render and redirect_to
[Rails] strftime this and that
[Rails] Search method when date and time have each column
Rails web server and application server
Difference between render and redirect_to
Difference between render and redirect_to
About Rails scraping method Mechanize
[Rails] How to get the URL of the transition source and redirect
[Rails] Why is it render if save is successful and redirect_to fails?
Difference between instance method and class method
Difference between == operator and equals method
[Rails] Save start time and end time
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
Difference between == operator and eqals method
[Rails] Difference between find and find_by
Proper use of redirect_to and render
[Java beginner] == operator and equals method
[Ruby basics] split method and to_s method
[Rails] Validation settings and Japanese localization
Rails model and table naming conventions
[Rails] About helper method form_with [Basic]
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Rails] Differences and usage of each_with_index and each.with_index
[Swift] Protocol concept and definition method
Project ruby and rails version upgrade
[Rails 6] destroy using the resources method
[Ruby on Rails] Convenient helper method
[Introduction to Rails] How to use render
Consideration about Rails and Clean Architecture