【问题标题】:Rails 3 - overwriting routesRails 3 - 覆盖路线
【发布时间】:2011-09-02 10:49:59
【问题描述】:

我目前在为我的应用程序设置正确的路由时遇到了一些问题。
简而言之,我希望有这样的 URL:

解释:
Show actual page for the selected poll
摘要:
localhost:3000/polls/:category_slug/:poll_id
示例:
localhost:3000/polls/technology/1337
routes.rb
get 'polls/:category_slug/:poll_id' => 'polls#show', :as => :poll

此外,用户应该能够根据某些条件过滤投票,例如 显示热门投票、新投票 等等...

说明:
Show a list of polls, which are matching the selected criteria
摘要:
localhost:3000/polls/:category_slug/:filter_mode
示例:
localhost:3000/polls/technology/top
routes.rb
get 'polls/:category_slug/:filter_mode' => 'filter#by_mode', :as => :polls_filter


这就是问题所在
ActiveRecord::RecordNotFound : Couldn't find Poll with ID=top_all

第二条路由 ('polls/:category_slug/:filter_mode') 覆盖了第一条路由,因此 Rails 将:filter_mode 识别为:poll_id。

所以我的问题是,我该如何改变这种行为,以便 两条路线实际上都可以
相互覆盖?
(当我离开时,第一条路线将完美运行出第二个)

我希望有人理解我的问题,感谢每一个帮助。

【问题讨论】:

  • 你为什么不使用 searchlogic 或其他提供类似功能并在参数中传递过滤条件而不触及路由的 gem?
  • 我没有使用 gem,因为我需要上面描述的 URL(例如 polls/technology/top,它会返回技术类别中排名靠前的民意调查列表)。我的方法是使用链接(调用我的过滤器控制器)进行过滤,而不使用搜索表单(在 URL 中传递类似“?category=1”的过滤条件)。不幸的是,我无法弄清楚如何使用 searchlogic gem 创建这样的 URL,所以我决定这样做^^
  • 感谢您满足我的好奇心 :)

标签: ruby ruby-on-rails-3 routing routes overwrite


【解决方案1】:

您可以对过滤器设置一个约束,使其仅接受字符串,然后将其放在另一个之前。它将落入 poll_id 。

【讨论】:

  • 感谢您的快速回答。像现在这样工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-28
  • 2018-06-13
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多