How to Use Rails 3 Routes with Dynamic Segments - ruby-on-rails

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_level as its own /-separated substring. When I switched to using the route declaration:
match "/:muscle_group/grade/:grade_level/:id" => "exercises#show"
Instead of the original:
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
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.

Action pack in Rails uses the to_param to override how the urls get generated by the URL helpers. Take a look at this article, which explains it.
http://www.seoonrails.com/to_param-for-better-looking-urls.html

Related

How can I take out the first segment of a route in rails 4 scaffold

I am trying to create a scaffold called 'Pages'.
So far everything is fine but the page structure now needs to change where it currently is:
http://0.0.0.0:3000/pages/the-page-name
What I need to do now is have this instead:
http://0.0.0.0:3000/the-page-name
In my routes.rb I have this:
resources :pages
This obviously maps all routes within the model to this base but I want to hide this.
Is it entirely possible?
Thanks,
Taken from the Rails Routing Guide, you could do this:
get '*pages', to: 'pages#show', format: false
I would recommend you make it the very last route you have, since the Rails router matches the request with the first route, and having a wildcard early in your routes file will end up clobbering all your other routes/resources.
You can specify a path
resources :pages, path: ''

Rails Routing and View Helper Names

I have this route defined in my routes.rb:
matc "articles/:id/vote/up" => "articles#vote", :liked=>true
The rake for this route is:
/articles/:id/vote/up(.:format) jokes#vote {:liked=>true}
With the first column being blank.
My question is, what would the route helper name be for this link?
For example articles_vote_up_path(:id).
Is there an uglier way to link to this path?
How can I find out?
I'm thinking I misunderstood something, please guide me in the correct direction in that case.
Thank You
you need to pass an :as option to use a named route
match "articles/:id/vote/up" => "articles#vote", :liked=>true, as: :article_vote_up
which generates an article_vote_up_path
When you run rake routes, you can add _path to the whatever's in the first column on the left, and that's what the route url helper is for that link. For example, here's a line from rake routes on one of my projects:
subscriptions GET /subscriptions(.:format) subscriptions#index
The url helper for this url would be subscriptions_path.

catching wild card route for missing routes, doesn't work when having additional routes files

I implemented a simple custom errors solution.
this one: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
everyhing is working fine except the missing routes in the routes.rb file..
in order to get to my error_controller when there is a missing route i did the wildcard solution: match '*not_found', to: 'errors#error_404'
but... now when i try to enter a sub section of my site which seats under:
/admin, i get to the error page. the wilcard gets triggered, even tough the route for admin section is defined in a different route file, under: config/routes/admin.rb
what can I do?
thanks
edit:
using rails 3.0.20 and ruby 1.8.7
If you're using Rails 3.2+, there is a simpler solution for your routes. First in 'config/application.rb' set your app as the error handler
config.exceptions_app = self.routes
Now when there is an your app will look to your routes to handle it. In 'config/routes.rb' you can add a route such as:
match "/404", :to => "errors#not_found"
A more verbose explanation can be found here.
OK so until I will update to Rails 3.2+
I simply put '*not_found', to: 'errors#error_404' into the last route file that is loaded.
that way its truly in the end of the routes and now all my routes work. and the error is still fired when needed.

Custom route for 'news' resource

I have a resource in a Rails 3.2 app called "news". I know it is not the best name to use for a resource but I'd prefer not to change it.
I have an issue with the routing since new_news_path doesn't work.
How can I define a custom route for the new action? something like unused_news_path for news#new?
thanks for your help.
Try this in your routes.rb:
match "unused" => "news#new"
When you do a rake routes | grep unused, you will get the following output:
unused /unused(.:format) news#new
So you now can use unused_path on your views and controller to get to the correspondent action.

Routing Error Ruby on Rails

I've installed ruby on rails 3.2.6, and when I execute
rails server
and access to 127.0.0.1:3000 it works, however when I generate a controller, for example
rails generate controller principal
and access to 127.0.0.1:3000/somecontroller, browser show following error:
Routing Error
No route matches [GET] "/principal"
Try running rake routes for more information on available routes.
What do I need to do, and can this be simply explained?
The problem is you did not specify any actions, so your controller 'principal' is empty, no views will be created with similar names, and no routes created.
You need to do:
rails generate controller principal index [show] [edit] [update] [create] [destroy]
The name after your controller name are the action names. Since you said controller 'principal' in the singular, then it might imply that you have a singular resource. If you want to have it in the plural, make sure you say 'controller principals'.
And your routes should should show:
resource :principal [ or :principals or multiple Restful routes ]
You need to edit config/routes.rb to tell the router which controller to route your request to. The rails standard is to use RESTful routes and in the example you've given this would equate to a singular Principal resource. Therefore you'd need to add:
resource :principal
to generate a set of RESTful routes for this resource. You can see the routes generated by doing:
rake routes
If you don't care about REST then you can simply add (assuming the PrincipalController has an index method):
match 'principal' => 'principal_controller#index'
Have a look at this chapter from the Rails Guides for more info on routing:
http://guides.rubyonrails.org/routing.html
You can also generate methods in the controller and routes at the same time by supplying their names as arguments to the rails generate controller command for example:
rails generate controller principal index

Resources