[RUBY] Difference between render and redirect_to, need for arguments

Introduction

Currently, we are implementing a flea market web application using rails. Among them, when implementing the function ** "Transition to the product list screen when the product can be saved" **, it is no longer necessary to add arguments to the URI (path) of redirect_to and render. I asked the mentor. The contents of the answers are summarized below.

1. Difference between redirect_to and render

The meanings of redirect_to and render are summarized with reference to actual usage examples. I think that it is easier to understand because it is summarized in the figure on the reference site at the bottom.

1-1. redirect_to: "Execution of action"

An example of use is a function that transitions to the product list screen after deleting a product (* root_path is the product list screen this time).

items_controller.rb


def index
  @items = Item.all.order('created_at DESC')
end

def destroy
  item = Item.find(params[:id])
  item.destroy
  redirect_to root_path  #Execute the above index action
end

Since redirect_to is the execution of the action of the specified URI, this time "Run root_path" = "Run root specified in routes.rb" = "Perform index action on items controller"

In this case, the @items in the index action will be regenerated to perform the index action.

If you use render here, @items will not be regenerated. By deleting, the data contained in @items will be changed, but since the transition to view is performed using @items before the change, NoMethodError as shown in the image below will occur. Error screen when using render

1-2. Render: "Call view"

An example of use is a function that transitions to the product list screen with redirect_to after saving the product, and displays the new post screen with render (= calls new.html.erb) if it cannot be saved.

items_controller.rb


def new
  @item = Item.new
end

def create
  @item = Item.new(item_params)
  if @item.save
    redirect_to root_path
  else
    render :new
  end
end

Since render is a call to the specified view, this time "Call new.html.erb"

In this case, you are calling new.html.erb inside the create action and the new action will not be executed. That is, @item in the new action is not generated. The reason for using render is that if you fail to save the data, you can redisplay the input screen while keeping the data you were inputting. By the way, you can confirm that "calling new.html.erb in the create action" because new disappears from the domain display if you dare to fail to save the product.

If you use redirect_to here, @item will be regenerated. Therefore, if the save fails, the new action will be executed again, and the data entered earlier will be updated and will be empty.

2. When the URI requires an argument

2-1. When redirect_to requires an argument

When using ** redirect_to **, it may be necessary to give an argument to the path. The image below is an example of executing the rais routes command in the terminal. If the ** URI Pattern contains an id like this blue part, you need to give an argument to the path. ** **

rails routes

The reason is that ** if there are many redirect destination paths, you must specify one path to execute the action with an argument **.

2-2. When render requires arguments

** If render requires arguments, it's basically not needed. ** ** The reason is that render is a reusable view, and the controller actions are the same, so you can use the already created instance in the render destination view as well.

3. Summary

--direct_to takes action again --render cuts out the view --When using redirect_to, if the URI contains id, you need to set the argument --Render basically requires no arguments

Reference site

--Layout and Rendering (Rails Guide)

--Difference between render and redirect https://qiita.com/january108/items/54143581ab1f03deefa

Recommended Posts

Difference between render and redirect_to, need for arguments
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] Difference between redirect_to and render
[Rails] Difference between redirect_to and render [Beginner]
[Rails] I investigated the difference between redirect_to and render.
[For beginners] Difference between Java and Kotlin
Criteria for proper use of render and redirect_to
Difference between vh and%
Difference between i ++ and ++ i
[Rails] Display error message-Differences between render and redirect_to, flash-
[Rails] What is the difference between redirect and render?
Difference between product and variant
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between CUI and GUI
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 List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
[Rails] Differences between redirect_to and render methods and how to output render methods
[Ruby] Difference between get and post
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
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
[Java] Difference between length, length () and size ()
[Android] Difference between finish (); and return;
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
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry