I have several publications (posts) which belong to a type and a category, and I want to have routes like this :
- /some-type-slug
- /some-type-slug/some-category-slug
- /some-category-slug
which all point to publications#index where I do all the filtering
As long as I tried only with type and optional category it worked just fine with :
scope ':type', :constraints => lambda{|req| PublicationType.all.map(&:slug).include?(req.params[:type].singularize) } do
match 'new' => 'publications#new'
match '(/:category)' => 'publications#index'
end
But when I added the same bit of code about categories, and I try to go to "/some-category" it ended just not working :
match ':category' => 'publications#index', :constraints => lambda{|req| Category.all.map(&:slug).include?(req.params[:category].singularize) }
leads to :
NoMethodError in PublicationsController#index
undefined method `singularize' for nil:NilClass
Parameters:
{"type"=>"some-category-slug"}
Ok so the NoMethodError is thrown because params[:category] is nil. And it is because the :category part of the url had just been "stolen" by the previous route definition : params[:type] is set with the :category part of the url.
So I wonder if this is a bug in the Rails router or if I'm just trying to push it a bit too far. And if I am I'd like to know what are my other options to achieve the same result.
Thanks !