Routes.rb not reflected in _path and _url

Viewed 38

I've defined custom URL paths in routes.rb, but my _path and _url helpers produce additional, incorrect routes.

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

Expected routes:

  • /custom/
  • /test/
  • /test/exam/

Actual routes:

  • /custom/
  • /test/
  • /test/exam/ (does load, but not defined _path or _url)
  • /exam/ (defined for _path & _url)

So all the desired routes do load, but the _path and _url helpers persistently display the wrong path for exam (/exam/) despite not being in routes.rb. :/

What am I doing wrong?

1 Answers

I assume the question is about exam_path and exam_url route helpers results. This is probably because of the way you call them.

Since there's additional parameters in path, you should pass them to helper too:

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