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.
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.
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.
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.
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. ** **
The reason is that ** if there are many redirect destination paths, you must specify one path to execute the action with an argument **.
** 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.
--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
--Layout and Rendering (Rails Guide)
--Difference between render and redirect https://qiita.com/january108/items/54143581ab1f03deefa
Recommended Posts