【问题标题】:Routes.rb not reflected in _path and _urlRoutes.rb 未反映在 _path 和 _url
【发布时间】:2019-01-10 15:06:57
【问题描述】:

我在 routes.rb 中定义了自定义 URL 路径,但我的 _path_url 助手会产生额外的错误路由。

routes.rb

get '/:id', to: 'groups#show', constraints: proc {|req| FriendlyId::Slug.where(sluggable_type: 'Group').pluck(:slug).include?(req.params[:id])}, as: :group
get '/:id', to: 'custom_pages#show', constraints: proc {|req| FriendlyId::Slug.where(sluggable_type: 'CustomPage').pluck(:slug).include?(req.params[:id])}, as: :custom_page
get ':group_id/:id', to: 'exams#show', constraints: proc {|req| FriendlyId::Slug.where(sluggable_type: 'Exam').pluck(:slug).include?(req.params[:id])}, as: :exam

预计路线:

  • /自定义/
  • /测试/
  • /测试/考试/

实际路线:

  • /自定义/
  • /测试/
  • /test/exam/(加载,但未定义 _path_url
  • /exam/(为_path_url 定义)

所以所有需要的路由都会加载,但 _path_url 帮助程序持续显示错误的考试路径 (/exam/) 尽管不在 routes.rb 中。 :/

我做错了什么?

【问题讨论】:

  • 如何称呼这些助手? exam_path(some_id, group_id:'test') ?

标签: ruby-on-rails


【解决方案1】:

我认为问题是关于 exam_pathexam_url 路由助手的结果。 这可能是因为您称呼他们的方式。

由于路径中有额外的参数,你也应该将它们传递给助手:

exam_path('exam', group_id: 'test') # => /test/exam

【讨论】:

  • 非常感谢 Vasfed,这成功了。具体来说:exam_url(exam.group, exam)
猜你喜欢
  • 2012-08-10
  • 2014-09-05
  • 2016-06-24
  • 2011-01-21
  • 2011-12-21
  • 1970-01-01
  • 2011-05-12
  • 2014-04-20
  • 2011-04-22
相关资源
最近更新 更多