Rails: Difference between resources and resources

At first

** Day 19 of Calendar Planning 2020 ** It's been about 3 months since I started studying programming, so I will leave a note of what I learned in the article as an output. I would be happy if I could help anyone entering the world of programming. Please let me know if there are any words that are wrong, wrong, or misunderstood ^^ I'm sorry if it's hard to read the words for a long time. I will do my best to get used to it little by little.

Difference between resources and resources

** The conclusion is resources: index and id are generated resource: index and id are not generated **

What is the resources method?

A method that automatically generates routes for the seven basic action names in rails.

routes.rb


get  "posts/index" => "posts#index"
post "posts"       => "posts#create"

Set the routing for each action like this. To be honest, it's annoying ^^;

That's when the resources method comes into play.

routes.rb


resources :posts

This one line automatically generates the routing for 7 action names. It saves a lot of trouble and speeds up work.

Terminal


$ rails routes

    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PATCH  /posts/:id(.:format)      posts#update
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy

If you check it on the terminal to check the routing, it will come out like this. What you should pay attention to here is that there is an index and /: id.

resource method

routes.rb


resource :posts

If you try to output in the same way like this

Terminal


$ rails routes

new_posts  GET    /posts/new(.:format)  posts#new
edit_posts GET    /posts/edit(.:format) posts#edit
     posts GET    /posts(.:format)      posts#show
           PATCH  /posts(.:format)      posts#update
           PUT    /posts(.:format)      posts#update
           DELETE /posts(.:format)      posts#destroy
           POST   /posts(.:format)      posts#create

I think it will be output like this.

The index and /: id are gone.

Finally

I used it at such a time

I used resource because I don't need a special id to add a like function to the post function. I used it when I didn't have to manage id as a user.

It may be good to use it properly according to the situation.

When I personally want to not manage IDs, I searched and found out, so I will write it down.

Recommended Posts

Rails: Difference between resources and resources
[Rails] Difference between find and find_by
[rails] Difference between redirect_to and render
[Rails] I learned about the difference between resources and resources
Difference between vh and%
Difference between i ++ and ++ i
[Rails] Difference between redirect_to and render [Beginner]
[Rails / ActiveRecord] About the difference between create and create!
Difference between member and collection of rails routes.rb
[Rails] Difference between create method and new + save method
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Rails] What is the difference between redirect and render?
[Rails] I investigated the difference between redirect_to and render.
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[Android] Difference between finish (); and return;
[Rails] What is the difference between bundle install and bundle update?
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
Rails and FormData
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium