In my routes.rb:
resources :posts do
get "test"
end
This produces the usual RESTful routes with /post/:id/...
. However, I also get /post/:post_id/test
.
Now my problem is that sometimes the parameter is named :id and sometimes it is :post_id. How can I make it uniform?
Thank you!
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.
Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.
Specify :on => :member, otherwise it is acting as a nested resource.
resources :posts do
get 'test', :on => :member
end
You shouldn't make it uniform. It's :id
when it's the target resource and :post_id
when it is the parent of some other target resource (i.e. nested resources). This is a Rails convention.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With