"rails routes" It's good to display this on the terminal, but I couldn't understand how to use it. So I summarized what I wrote in my notebook.
The concept of MVC is needed in programming. When there is a request from the client at that time, define the destination corresponding to the request.
This time I would like to set the first root method.
sample.rb
Rails.application.routes.draw do
root to: "posts#index"
end
Terminal
rake routes(rails routes is also possible)
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
root to: 'Controller name#Action name'
'Controller name # Action name'
This quotes Controller # Action of rails routes as it is.
Then use "" (double quotes) and it's done.
root to: "posts#index"
As a result, if you search on google etc. and click HP, the top page will be displayed.
link_to.rb
= link_to prefix name_path,HTTP method name class name do
sample.rb
link_to root_path do
link_to "Our Blog", root_path, class: "header__title--text" do
link_to "New post", new_post_path, class: "header__right--btn" do
Below is the result from the rails routes terminal
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
posts POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
GET means to display the form and DELETE means to delete. Please refer to the following. The location is described in Verb in rails routes.
However, if the Prefix is the same, it will be omitted from the second line.
In this case, if you do not specify method :: HTTP method name
, an error will occur, so be sure to describe it.
sample.rb
<%= link_to 'Edit', tweet_path(@tweet.id), method: :get %>
<%= link_to 'Delete', "/tweets/#{@tweet.id}", method: :delete %>
Recommended Posts