** 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.
** The conclusion is resources: index and id are generated resource: index and id are not generated **
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.
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.
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