routes - How can I write Rails 4 routing namespaces without duplicating the controller name? -
routes - How can I write Rails 4 routing namespaces without duplicating the controller name? -
what i'd able write like:
namespace :cats '/:breed/' => '#search_by_breed' '/:breed/count' => '#count_by_breed' end
but leads these routes:
/cats/:breed(.:format) cats/#search_by_breed /cats/:breed/count(.:format) cats/#count_by_breed
which, given slash in cats/#show
, won't work.
i know can following:
'/:breed/' => 'cats#search_by_breed' '/:breed/count' => 'cats#count_by_breed'
which results in these routes:
/:breed(.:format) cats#search_by_breed /:breed/count(.:format) cats#count_by_breed
however, duplicates naming cats
controller each time, i'd rather not (in actual code have more routes 2 listed above. unfortunately i'm not working on cat search site...). thought there'd away around this.
am missing something? on doing dry commandment? seems reasonably common, didn't find similar in routing docs.
to reply primary question, 1 way write routes accomplish want:
resources :cats, only: [], path: ":breed" collection 'search_by_count', path: 'count' 'search_by_breed', path: '/' end end
this generate next routes:
search_by_count_cats /:breed/count(.:format) cats#search_by_count search_by_breed_cats /:breed(.:format) cats#search_by_breed
of course, can still utilize separate declaration of resources :cats
more restful versions of index, show, etc.
to reply secondary question, "am overdoing dry commandment?", that's largely subjective matter. in experience, have seen many cases take dry far @ expense of readability , resulting in increased coupling. find deciphering next requires less mental overhead:
get '/:breed/' => 'cats#search_by_breed' '/:breed/count' => 'cats#count_by_breed' '/:breed/another' => 'cats#another_route'
for more thoughts on beingness sensible dry, offer article http://www.infoq.com/news/2012/05/dry-code-duplication-coupling , talk http://www.confreaks.com/videos/434-rubyconf2010-maintaining-balance-while-reducing-duplication
ruby-on-rails routes
Comments
Post a Comment