[Feature request][ActionPack::ActionDispatch::Routing] Add parameter for resources to alias "only: []" in routes.rb

17 views
Skip to first unread message

François Belle

unread,
Jan 17, 2018, 6:43:41 PM1/17/18
to Ruby on Rails: Core
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!

Nicholas Schwaderer

unread,
Jan 18, 2018, 8:00:13 AM1/18/18
to Ruby on Rails: Core
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.

Kevin Deisz

unread,
Jan 18, 2018, 8:27:15 AM1/18/18
to rubyonra...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Kevin D. Deisz

Rob Biedenharn

unread,
Jan 18, 2018, 10:15:52 AM1/18/18
to rubyonra...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Kevin Deisz

unread,
Jan 18, 2018, 11:12:43 AM1/18/18
to rubyonra...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.



--
Kevin D. Deisz

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages