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.

8 Responses to “Beginning Ruby On Rails: Route A Custom Controller Action”

  1. Tony West Says:

    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.

    • openlandscape Says:

      @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.

  2. maggie Says:

    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?

  3. Kumi Says:

    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!! :-)

    • openlandscape Says:

      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 :-) ;-)

  4. Mike L Says:

    That is so on the money. Worked beautifully the first time. This was not intuitive at all to figure out. Excellent and useful post.

  5. german Says:

    Thank you very much, yes sir, exactly what I was looking for.


Leave a Reply