Suddenly, when I first saw redirect_to
and render
, I didn't really understand the difference between them.
Let's dig deep and learn together!
redirect_to: Sends an HTTP request to the server and the user sees the HTML returned from it. render: A method that specifies the View file to call in the action.
I think that there are some parts that are difficult to understand with just words, so I will explain each ** processing flow **.
redirect_processing flow of to
① redirect in controller processing_Execute to
②redirect_HTTP request to the URL specified by the to argument(GET!!)Run
③ HTTP request(GET!!)Executes the routing process corresponding to the URL
(4) The controller and action corresponding to the routing process are called and the process is executed.
⑤ Render the view according to the process
① controller (redirect_to) → ② HTTP request (GET !!) → ③ routing → ④ controller → ⑤ view
Render process flow
① Execute render in the process of controller
(2) Display the view file specified by the render option
①controller(render) → ②view
It should be noted here that redirect_to is specified to be the route of ** GET method **.
You see it once in a while. A controller like the one below.
controllers/books.rb
class BooksController < ApplicationController
def index
end
end
If nothing is mentioned in the action, render will be executed automatically! !!
So in the above case, it's likely that the render'index''
has been omitted, and this action will call views / books / index.html.erb
.
Now let's take a look at some commonly used examples of redirect_to and render!
controllers/books.rb
class BooksController < ApplicationController
def create
@book = Book.new(book_params)
@book.user_id = current_user.id
if @book.save
redirect_to books_path, flash[:notice] = "Posting successful! !!"
else
flash.now[:alert] = "Posting failure!!"
render 'new'
end
end
end
When posting, if the result of the save method is true, it redirects to the post list page, and if false, the page for new posts is redisplayed.
render'action name''
.
You can also display the views of other controllers by specifying the controller name.Did you notice that you didn't call any action when viewing the view file using render?
↑ render'new'
only displays books / new.html.erb
, not via the new action.
In this pattern, I want to display a new page because posting failed, but if I use redirect_to, I call a new action and pass an empty method to the instance variable, so new The action will be reset and the data entered by the user will be erased. Therefore, the information for issuing the error message will also disappear, and the user will not be able to see "why could not post".
It's complicated, but it determines an important turning point, so I hope you'll face it until you're hungry.
I won't go into details because it's far from the main line, but if you want to send a message in the displayed view, write as follows.
redirect_In the case of to
flash[:notice] = "message"
Or
notice: "message"
For render
flash.now[:alert] = "message" #now is on! !! !! !!
I explained the difference between redirect_to and render, which is easy for beginners to make mistakes! !! In the explanation, I often learn by myself, and I am keenly aware of the importance of output.
Recommended Posts