[RUBY] [Rails] resources, member, collection Routing/named path/how to remember URL [for beginners]

Introduction

Nice to meet you. My name is Nagaemon. It took me more than 3 months to finally climb the mountain called Rails Tutorial, so

I summarized about.

While the paths/URLs generated by resources are very useful, There are many scenes where people are confused with "Oh, the named path is user_path? Users_path?" And "URL is/users?/User? /: Id?", And many people should have gone around the tutorial with somehow moody ( …right?).

I hope it will be helpful to all of our fellow beginners.

[environment] ruby 2.6.3 Rails 6.0.3.4

7 RESTful routings (basic)

The resource name is (as usual) the users resource.

routes.rb


resources :users

The seven routes generated by implementing this code Looking in order of action

action name HTTPreq URL Named path
users#index GET /users users_path
users#show GET /users/:id user_path
users#new GET /users/:id/new new_user_path
users#create POST /users users_path
users#edit GET /users/:id/edit edit_user_path
users#update PATCH/PUT /users/:id user_path
users#destroy DELETE /users/:id user_path

It's like this. (* The action name follows the notation of controller name #action name)

As for the seven action names and their order ... do your best to remember them. As a little reminder, index and show are a pair. Let's remember index as a set (whole) of users, and show as a "function to display" individual users. new and create are also paired. Based on the information entered in the new template form, it will often be connected to the create action to create a user instance. Edit and update are also paired. Based on the information entered in the form of the edit template, it will often be connected to the update action to update the information of the user instance. Finally destroy. This is a lone action you without a pair.

Relationship between URL and named path

Next, let's understand two naming rules: URLs and named paths.

[Rule ①] /: Classified by the presence or absence of id First of all, the URL starts with/users. This is plural and unified. Classify according to whether /: id is present or not after it.

/: Without id → Named path is "plural" users \ _ path With /: id → Named path is "singular" user \ _ path

[Rule ②] The option attached to the end of the URL is at the beginning of the sentence in the named path. The options mentioned here are

/users/○○ /users/:id/○○

The XX part above.

The named path of the URL of this pattern is based on rule ①

Option name \ _users \ _path or It will be option name \ _ user \ _path.

In this way, it is named by the rule that the option name comes at the beginning.

If you know the above two rules, The named path for/users /: id/new is new_user_path

Similarly The named path for/users /: id/edit is edit_user_path

You can understand that.

(Remember, this rule also applies to arbitrary routing generated by members and collections that will appear later!)

Reorganized 7 routes from named paths

Now let's reorganize the routing by the named path we just learned.

Named path HTTPreq URL React action
users_path GET /users users#index
POST /users users#create
new_user_path GET /users/:id/new users#new
edit_user_path GET /users/:id/edit users#edit
user_path GET /users/:id users#show
PATCH/PUT /users/:id users#update
DELETE /users/:id users#destroy

It will be like this.

Two of index and create are users_path. Remember this as an action on a collection of users. index: View the entire collection create: Add one instance to the entire collection

The other five are based on user_path. new and edit are named paths with options, as we saw earlier.

By arranging from two directions in this way, has the understanding deepened considerably?

By the way, when you run $ rails routes, the routes will be displayed for each "named path" here, so if you take a closer look, you will get a better understanding.

member and collection methods

Next, the member method and collection method that add arbitrary routing to the users resource. Let's also look at the URLs and named paths generated by these two methods.

These are passed (nested) to resources: users as blocks.

routes.rb


resources :users do
  member do
    get :foo, :bar
  end
  collection do
    get :hogehoge
  end
end

Characteristics of each method: The member method adds an action to "user_path". The collection method adds an action to "users_path".

How to write a method: Notice the lines get: foo,: bar. It is written as "HTTP request type: arbitrary action name,: arbitrary action name".

In the above example, users # foo, users # bar, users # hogehoge Three actions (methods) called are generated.

The routing corresponding to each action is

"Action name = URL and named path options"

It is easy to understand if you think about it. Since it is an option, it was a rule that the URL comes to "behind" and the named path comes to "beginning".

Including generated foo action, bar action, hogehoge action Reorganize routing by named path.

Why would it be such a named pass for all of you today? You should be able to understand why the URL is like this.

Named path HTTPreq URL React action
hogehoge_users_path GET /users/hogehoge users#hogehoge
foo_user_path GET /users/:id/foo users#foo
bar_user_path GET /users/:id/bar users#bar
users_path GET /users users#index
POST /users users#create
new_user_path GET /users/:id/new users#new
edit_user_path GET /users/:id/edit users#edit
user_path GET /users/:id users#show
PATCH/PUT /users/:id users#update
DELETE /users/:id users#destroy

That is all.

Thank you for staying with us until the end. I think it was a poor content, I hope this article helps anyone who was confused by the named path.

Thank you again: runner:

Nagaemon

Recommended Posts

[Rails] resources, member, collection Routing/named path/how to remember URL [for beginners]
[Rails] Add page nation to table [For beginners]
[Rails] How to create a signed URL for CloudFront
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Tutorial to create a blog with Rails for beginners Part 1
Tutorial to create a blog with Rails for beginners Part 2
Tutorial to create a blog with Rails for beginners Part 0
[Rails] How to display error messages for comment function (for beginners)
(For beginners) [Rails] Install Devise
[RSpec on Rails] How to write test code for beginners by beginners
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Must-see for beginners] rails_12factor is not required to publish Rails app to Heroku
(For beginners) [Rails] Time saving tech! How to install and use slim
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Rails] Articles for beginners to organize and understand the flow of form_with