I would like to propose a feature that would be kind of an alias for
resources :users, only: do
…
end
``
I thinks it’s weird to have to specify an empty array when we need a resourceful route without the default CRUD generated along.
It would allow us to use it like the shallow feature:
resources :users, without_crud: true do
get :unsubscribe, on: :collection
get :foo
post :bar
end
``
And generate only:
GET /users/unsubscribe
GET /users/:id/foo
POST /users/:id/bar
``
Any feedback would be greatly appreciated, thank you guys!
I feel that only: [] definitely is not an intuitive approach that shows what you would be trying to achieve in that instance.
Also, I believe your only current alternative would be writing out your resourcing ‘longhand’ outside of an actual resources block in your routes.rb, correct? (I have seen all too many apps bloat with a lot of handscrawled routes)
But since this is a bit aways from normal resources namespace usage maybe your alias ought to go all the way up to resources itself… such as:
resources_without_crud :users do
...
end
When I think of resources I think of the crud rails magic, and maybe it would help to make that even more explicit. Very interesting thought and thank you for raising it.
You can achieve this functionality by throwing this into the top of your routes drawing block:
def subresources(name, &block)
resources(name, only: [], &block)
end
since that block is getting instance_eval’d anyway, this will just put that method onto the routes object. Then you can:
subresources :users do
...
end
and it’ll have the same effect.
If you don’t want the resources anyway, why not just use namespace:
==> config/routes.rb <==
Rails.application.routes.draw do
namespace ‘users’ do
get ‘unsubscribe’
get ‘:id/foo’, action: :foo, as: :foo
post ‘:id/bar’, action: :bar, as: :bar
end
end
Prefix Verb URI Pattern Controller#Action
users_unsubscribe GET /users/unsubscribe(.:format) users#unsubscribe
users_foo GET /users/:id/foo(.:format) users#foo
users_bar POST /users/:id/bar(.:format) users#bar
-Rob
Namespace is semantically different in that sub resources are nestled under an instance of the resource they represent, not just a static string.
i.e., /resource/1/subresource, not /resource/subresource