I summarized the points to note when using resources and resources in combination

The other day, I summarized the differences between resources and resources, but there were some points to note when using them in combination, so I summarized them. Click here for the difference → Difference between resources and resources --Qiita

When using resources in combination with resources

By using resources in combination, you can easily generate the following routing.

--List display of all users --Detailed display of all users --Detailed display of own user --Editing your own user information

Anyone can view the list of all users and the details of each user, but only their own can edit user information.

config/routes.rb


  resources :users, only: [:index, :show]
  resource :user, only: [:show, :edit, :update]

Looking at the routes generated by rails routes, it looks like the following.

Terminal


   Prefix Verb  URI Pattern          Controller#Action
    users GET   /users(.:format)     users#index
     user GET   /users/:id(.:format) users#show
edit_user GET   /user/edit(.:format) users#edit
          GET   /user(.:format)      users#show
          PATCH /user(.:format)      users#update
          PUT   /user(.:format)      users#update

ʻUser GET / users /: id (.: format) The user_path part of users # show` is assigned to / users /: id, so the helper method that should return / user is not generated. So, I tried to describe it in reverse order.

config/routes.rb


  resource :user, only: [:show, :edit, :update]
  resources :users, only: [:index, :show]

Looking at the routes generated by rails routes, it looks like the following.

Terminal


   Prefix Verb  URI Pattern          Controller#Action
edit_user GET   /user/edit(.:format) users#edit
     user GET   /user(.:format)      users#show
          PATCH /user(.:format)      users#update
          PUT   /user(.:format)      users#update
    users GET   /users(.:format)     users#index
          GET   /users/:id(.:format) users#show

If the local user can be identified by the login function (session management), it is not necessary to include: id in the request, so the routing for the local user uses resource.

In order to see the details screen of users other than yourself, it is necessary to specify the ID of the target user, so resources is generating a route that includes: id in the request.

Conclusion

When creating a route by combining resource used in the singular and resources used in the plural, write the singular resource above.

Recommended Posts

I summarized the points to note when using resources and resources in combination
Unable to get resources when using modules in Gradle and IntelliJ
I stumbled when I tried using neo4j in the jenv environment, so make a note
I can't log in to MySQL from Django when using docker-compose
Things to note when using Spring AOP in Jersery resource classes
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to solve the unknown error when using slf4j in Java
[Rails] I was addicted to the nginx settings when using Action Cable.
I tried to summarize the stumbling points when developing an Android application
A story I was addicted to when testing the API using MockMVC
I tried to summarize the key points of gRPC design and development
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I tried to organize the session in Rails
When the server fails to start in Eclipse
How to use Segmented Control and points to note
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.
A note when I'm addicted to using Docker Hub Vault in server mode
[JDBC ③] I tried to input from the main method using placeholders and arguments.
Be careful with requests and responses when using the Serverless Framework in Java
I want to get the value in Ruby
I want to issue a connection when a database is created using Spring and MyBatis
I tried to summarize the points to consider when acquiring location information with the iOS application ③
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
I tried to summarize the points to consider when acquiring location information with the iOS application ①
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
I tried to summarize the points to consider when acquiring location information with the iOS application ②
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
I was addicted to the NoSuchMethodError in Cloud Endpoints
I tried to organize the cases used in programming
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
# 2 [Note] I tried to calculate multiplication tables in Java.
I want to judge the range using the monthly degree
Things to keep in mind when using if statements
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
How to convert A to a and a to A using AND and OR in Java
I want to call the main method using reflection
I tried to implement the Euclidean algorithm in Java
Differences in code when using the length system in Java
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
In Java, I want to trim multiple specified characters from only the beginning and end.
I was trapped when I generated my own class equals and hashCode in Java using IDE
How to solve the problem when the value is not sent when the form is disabled in rails and sent
I got an error when using nextInt, nextLine and substring.
How to test including images when using ActiveStorage and Faker
What I was addicted to when introducing the JNI library
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
How to set chrony when the time shifts in CentOS7
I tried to summarize the basics of kotlin and java
I want to bring Tomcat to the server and start the application
I want to call a method and count the number
I tried JAX-RS and made a note of the procedure
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I have summarized the articles that programming beginners referred to.
I tried to build the environment little by little using docker
Contributed to Gradle and was named in the release notes