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. .. ..
【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
① 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
Leave the task name blank so that it will not be saved due to validation. Now when you press "Create Task" ... 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
Again, save the task name in the blank on purpose. When you press "Create Task" ... 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.
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.
Now press Create Task. .. .. 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. Press Create Task. .. The task was saved and moved to the index view.
Recommended Posts