[Rails] How to decide the destination by "rails routes"

"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.

root method

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.

image.png

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                                                                           
                   

How to write root is as follows

root to: 'Controller name#Action name'

Commentary

'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.

[Rails] How to jump to the view file specified by the link_to method

How to write

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

What is an HTTP method?

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. “スクリーンショット” 2020-10-20 15.55.07.jpg

However, if the Prefix is the same, it will be omitted from the second line. “スクリーンショット” 2020-10-20 15.02.03.jpg

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

[Rails] How to decide the destination by "rails routes"
How to read rails routes
How to remove the underline displayed by Rails link_to
[Rails] How to display the list of posts by category
[Rails] How to use the map method
How to write Rails
How to output CSV created by Rails to S3
How to uninstall Rails
How to separate .scss by controller in Rails
How to make the schema of the URL generated by Rails URL helper https
How to check Rails commands in the terminal
[Ruby on Rails] How to make the link destination part of the specified id
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to set the display time to Japan time in Rails
[Rails] How to search by multiple values ​​with LIKE
How to return to the previous screen by Swipe operation
How to move to the details screen by clicking the image
[Ruby on Rails] How to change the column name
[Rails] I don't know how to use the model ...
[Rails] How to change the column name of the table
[Rails] How to get the contents of strong parameters
[Rails] How to display an image in the view
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to dynamically change the column name acquired by MyBatis
How to connect the strings in the List separated by commas
[With back tricks] How to introduce React to the simplest Rails
[Rails] How to delete images uploaded by carrierwave (using devise)
Let's summarize how to extend the expiration date of Rails
How to run React and Rails on the same server
[Rails] How to display information stored in the database in view
How to use the link_to method
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
How to find the average angle
[Rails] How to prevent screen transition
Explanation of the order of rails routes
How to use the wrapper class