Rails 3 - 覆盖路线
我目前在为我的应用程序设置正确的路线时遇到一些问题。
简而言之,我希望有这样的网址:
说明:显示所选投票的实际页面
摘要:localhost:3000/polls/:category_slug/:poll_id
示例:本地主机:3000/polls/technology/1337
路线.rb获取 'polls/:category_slug/:poll_id' => '民意调查#show', :as => :poll
此外,用户应该能够根据某些条件过滤民意调查,例如显示热门民意调查、新民意调查等等...
说明:显示符合所选条件的民意调查列表
摘要:localhost:3000/polls/:category_slug/:filter_mode
示例:localhost:3000/polls/technology/top
路线.rbget 'polls/:category_slug/:filter_mode' =>; 'filter#by_mode', :as =>; :polls_filter
问题就在这里ActiveRecord::RecordNotFound:找不到 ID=top_all 的投票
第二条路线 ('polls/:category_slug/:filter_mode'
) 会覆盖第一条路线,以便 Rails 将 :filter_mode
识别为 :poll_id
。
所以我的问题是,我怎样才能改变这种行为,所以两条路线实际上都会工作
而不会互相覆盖?(当我省略第二条路线时,第一条路线将完美地工作一)
我希望有人理解我的问题,感谢每一个帮助。
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.rbget '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.rbget 'polls/:category_slug/:filter_mode' => 'filter#by_mode', :as => :polls_filter
And here's the problemActiveRecord::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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以对过滤器设置一个约束,使其仅接受字符串,然后将其放在另一个过滤器之前。它将落入 poll_id 的位置。
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.