Beginning Ruby On Rails: Route A Custom Controller Action
January 28, 2009
I recently rewrote my pet project in Ruby on Rails: Again! A very long time ago, in a galaxy far far away (aka Johannesburg, since I’m now staying in Cape Town), the project was done in Python, and later I converted it to Java and Google Web Toolkit – ’cause I thought GWT kicked serious ass. It wasn’t going too bad with the Java-GWT affair, but somehow I never felt completely in love with GWT’s philosophy. I like designing HTML pages, and prefer not to have to code my UI in Java. Coding a UI as a bunch of classes, and not laying it out in design medium like HTML, is really not my cup of tea. After a recent project based on ASP .NET MVC and jQuery that turned out as one of my favorite projects ever, I realized that, personally, this is the way to go.
So with a lot of personal reluctance, I thought I’d give one of the most famous MVC web frameworks a try and see how it goes: Ruby On Rails. And man, am I impressed: I really enjoy scaffolding, DB migration, and how you can incrementally generate models, and controllers. The way which model objects can automatically accommodate any query via the method_missing mechanism is also wicked. And the fact that I don’t need to know anything but Ruby to achieve it all, is a real bonus. I have done work in Rails in a couple of minutes, that will take me hours to do in Java and GWT. And I’m only an amateur Ruby developer at this stage. Anyways, just thought I’d give a little background as to how I ended up writing about Ruby.
Routing a call to a custom controller action wasn’t too clear from the official Rails documentation, and I had to do a little more Googling than I thougt is acceptable.
In my case I wanted to add a custom controller action called “pick” that should display web_feeds/pick.html.erb:
class WebFeedsController < ApplicationController def pick end end
This will produce the error:
“ActiveRecord::RecordNotFound in WebFeedsController#show
Couldn’t find Post with ID=pick“
From this error message we infer that Rails thinks we are requesting the “show” view with WebFeed ID “pick”, which is not what we really want. Routes are defined in config/routes.rb. To view all the available routes for your application run rake routes in the shell. Routes defined before others get a higher priority. So you need to define more specific routes before the general/default routes. When you look at routes.rb you’ll notice lines beginning with map.resources :controller_name. Map.resources generates routes for seven default actions (index, show, create, new, edit, update, and destroy). So what we need to do is define a more specific route to our other custom actions, before these default routes:
map.connect "web_feeds/:action", :controller => 'web_feeds', :action => /[a-z_]+/i
This line will map to any custom action on WebFeedsController. The :action argument is a regular expression that accepts any word and underscores. This stops Rails from trying to map an ID for one of the default routes to an action. “i” Makes the regex case insensitive.






March 10, 2009 at 6:06 am
This was excellent; thank you very much. I find RoR to be a powerful technology, however it is not without its drawbacks. The biggest one that I have found is that it is difficult to find the documentation that one needs to learn the basics of Rails.
This post was clear, concise, and exactly what I needed.
March 11, 2009 at 4:15 pm
@Tony. Glad to be of help. I believe to some extent Ruby lacks a central consistent, comprehensive reference source, like MSDN for Microsoft’s technologies. However, I count Ruby’s enormous and passionate community as a huge benefit. I believe the community is so big, and there are so many people out there covering it, that a central source like MSDN is not necessary.
This forces us to spend a little more time Googling, but in the end we definitely find what we’re looking for. Like what happened with you finding my blog post
Cool. Happy Rubying.
May 30, 2009 at 7:38 pm
THANK YOU. I have been looking everywhere in Learning Rails as well as online. This is EXACTLY what I needed. Why can’t they explain this in the beginner books?
June 1, 2009 at 7:59 pm
@maggie. Maybe I’ll write a Rails book in a few years time …
June 15, 2009 at 9:35 pm
Thanks so much for adding this post! I could not find this info anywhere else, so I googled with “added action to controller ruby on rails not seeing” and your site came up at the top! Saved me hours of searching! Thanks a lot!!
June 18, 2009 at 4:05 pm
My pleasure. Glad I could help you, that is after all the main aim of my blog & to become a legendary web 2.0 blogger
June 21, 2009 at 7:38 pm
That is so on the money. Worked beautifully the first time. This was not intuitive at all to figure out. Excellent and useful post.
June 22, 2009 at 3:40 am
Thank you very much, yes sir, exactly what I was looking for.