Rails render redirect_to

Introduction

This time about the difference between render and redirect_to, which are often used in Rails.

render render determines the view file in the controller and renders it.


render :edit

In this case, the html file corresponding to edit of the view file is called.

Call from another controller

It is possible to call from another controller, in which case you can pass the full path starting from app / views, so specify the template you want to output with the full path.


render "users/show"

You can call the view template from ʻapp / views / users`.

Specify any file

It can be specified even if it is outside the current directory.


render "/xxx/apps/user_directory/current/app/views/users/show"

Various description methods

render :edit render action: :edit render "edit" render "edit.html.erb" render action: "edit" render action: "edit.html.erb" render "books/edit" render "books/edit.html.erb" render template: "books/edit" render template: "books/edit.html.erb" render "/path/to/rails/app/views/books/edit" render "/path/to/rails/app/views/books/edit.html.erb" render file: "/path/to/rails/app/views/books/edit" render file: "/path/to/rails/app/views/books/edit.html.erb"

https://railsguides.jp/layouts_and_rendering.html

redirect_to To instruct the browser to resend the request to another URL. Pass the url you want to redirect to as an argument.


redirect_to users_url
redirect_to "http://www.~"

Specify controller


redirect_to  controller: :users, action: :edit

Redirect to previous page

Redirect from the current page to the previous page.

redirect_to :back

Change redirect status code

By default, redirect_to returns HTTP status code 302 to the browser and redirects. If you want to make changes

redirect_to users_path, status: 301

Added message when redied

redirect_to users_path, notice: 'Added

About the difference between render and redirect_to

For the process that render only specifies the view file and displays it redirect_to resends the request to another URL and makes the browser work.

The difference between displaying a view file and making an http request.

Render can be used to return to the previous screen, acquire data, and display it. redirect_to includes updating and deleting data.

Recommended Posts

Rails render redirect_to
[rails] Difference between redirect_to and render
[Rails] Difference between redirect_to and render [Beginner]
Difference between redirect_to and render
[Rails] Display error message-Differences between render and redirect_to, flash-
Difference between redirect_to and render
Rails "render method" and "redirect method"
Difference between render and redirect_to
[Rails] I investigated the difference between redirect_to and render.
Difference between render and redirect_to
Difference between render and redirect_to
[Rails] Differences between redirect_to and render methods and how to output render methods
Difference between render method and redirect_to
Proper use of redirect_to and render
[Introduction to Rails] How to use render
[Rails] Why is it render if save is successful and redirect_to fails?
[Rails g.error]
Rails Review 1
Rails API
Rails migration
[Rails] first_or_initialize
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
[Ruby on Rails] How to use redirect_to
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Difference between render and redirect_to, need for arguments
Criteria for proper use of render and redirect_to
One way to redirect_to with parameters in rails