In my Rails 3.2 application, there is an Exercise model with attributes muscle_group and grade_level. I’ve defined the following route with dynamic segments for it in config/routes.rb:
# config/routes.rb
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
Running bundle exec rake routes confirms that the route does indeed exist:
/:muscle_group/grade-:grade_level/:id(.:format) exercises#show
The database contains an Exercise record with:
- id = 5
- muscle_group = “abdominal”
- grade_level = 1
And yet when I point my browser to http://localhost:3000/abdominal/grade-1/5, I get:
Routing Error
No route matches [GET] "/abdominal/grade-1/5"
Try running rake routes for more information on available routes.
How can I get this route with dynamic segments to work?
The route declaration I was using was almost functional. For some reason there is an issue with using a hyphen and not having
grade_levelas its own/-separated substring. When I switched to using the route declaration:Instead of the original:
http://localhost:3000/abdominal/grade/1/5 is then a valid route.
I would prefer to have this route with the hyphen and wish I knew how to make that work, but it is not a top priority. If anyone knows how to make it work with the hyphen, please let me know.