I’m currently having some problems with setting up the right routes for my application.
In short I would like to have URLs like so:
explanation:
Show actual page for the selected poll
abstract:
localhost:3000/polls/:category_slug/:poll_id
example:
localhost:3000/polls/technology/1337
routes.rb
get 'polls/:category_slug/:poll_id' => 'polls#show', :as => :poll
Furthermore the user should be able to filter the polls against some criteria, like show Top-Polls, New-Polls and so on…
explanation:
Show a list of polls, which are matching the selected criteria
abstract:
localhost:3000/polls/:category_slug/:filter_mode
example:
localhost:3000/polls/technology/top
routes.rb
get 'polls/:category_slug/:filter_mode' => 'filter#by_mode', :as => :polls_filter
And here’s the problem
ActiveRecord::RecordNotFound : Couldn't find Poll with ID=top_all
The second route ('polls/:category_slug/:filter_mode') is overwriting the first route so Rails recognizes the :filter_mode as a :poll_id.
So my question is, how can I change this behavior, so both routes will actually work
without overwriting each other ? (the first route will work perefectly, when I leave out the second one)
I hope someone understands my problem, appreciate every help.
You can set a constraint on the filter one to take only strings, and then put it before the other one. It will fall through to the poll_id one.