Rails Custom URL Tutorial

May 18th, 2006

When my del.icio.us rails app finally goes live, one of the features it will have will be the ability for users to associate a retreived query with a keyword so that they can easily get back to it in the future.

For instance, let's say that I get a list of my del.icio.us links tagged with the word "ruby." I edit them in whatever way I feel like, and then save my creation along with a keyword that I choose - let's say "ruby" for simplicity.

I don't want my users to have full access to the admin sections of my application - they'll only have access to create and update their query results. So, I want to give them the ability to use their keyword as part of the url. So it'll be something like http://delipaste.com/saved/ruby.

The default for URLs in rails though is something like http://delipaste.com/controller_name/action_name. I don't want a controller named "saved." For this example though, let's say that I don't want to create a new controller. I also don't want a seperate action for each of my keywords. How do we set up a custom URL like this then?

Map.connect to the rescue! This particular rails magic occurs in the routes.rb file which is in your application's config directory. Here's what's in there as a default:
1
2
3
4
5
6
7

  # Allow downloading Web Service WSDL as a file with an extension
  # instead of a file named 'wsdl'
  map.connect ':controller/service.wsdl', :action => 'wsdl'
  
  # Install the default route as the lowest priority.
  map.connect ':controller/:action/:id'
The pupose of this file is to allow us to create our own, custom URLs. Here's the one that I created:
1
2
3
4

  map.connect 'saved/:keyword',
                    :controller => 'get',
                    :action     => 'saved_query'
Here's how to read it:
If we get in a URL of form appname/saved/sometext, then we want to call the "saved_query" method in the controller named "get," passing in the value after the slash. In my "get" controller, here's what my " saved_query" method looks like:
1
2
3
4
5
6
7

  def saved_query
    keyword = params[:keyword]
    @query = Query.find( :all,
                                  :conditions => ["keyword = ?", keyword])
    redirect_to :action => 'edit', :id => @query[0].id
  end
It's relatively straightforward, I think. It just grabs the keyword, "ruby" in this case, and then queries the table for rows where keyword='ruby.' Then it passes the retrieved information to the 'edit' page where the user can view or edit their creation. My application has validation code to ensure that each keyword is unique. The query will never return more than one row.

I guess what I've really done is given the user a way to retrieve their information without needing to know ids or anything else about the application (the names of my controllers, other methods, etc. I think that redirecting the user to the "edit" page is probably a poor choice - I should probably use the form info from the edit page, but make it more suitable to my users. It works for my testing though, which is really what I'm interested in for this phase of development. Obviously I have some security issues I need to deal with to make sure users don't access restricted parts of the system, but that's all for future development.

Eventually I intend on making all of the source code from these tutorials available here at RubyNoob. It's nothing terribly exciting, but it might be educational for someone. As usual, please feel free to leave comments if I've really screwed something up or if you know a much better way of doing things.

Here's a quick video demo which might help:

10 Responses to “Rails Custom URL Tutorial”

  1. טיסות Says:
    The idea that the user doesnt have to have knowledge and the things are transparent as far as he concerned, this is great, while I agree that redirecting to the home page is quite a poor choice...
  2. Recombinant protein Says:
    If we get in a URL of form appname/saved/sometext, then we want to call the "saved_query" method in the controller named "get," passing in the value after the slash
  3. in Ultram Says:

    Hi! ultram [url=http://www.networkingitalia.it/modules.php?name=Forums&file=viewtopic&p=26352&c-topic=ultram]ultram[/url] Waiting for you!

  4. Walter Says:

    Hi there. I would really like to hear how you integrated Google Checkout into Ruby on Rails. Have you published an article somewhere? Would you send me a link please? Thanks in advance. Walter.

  5. צ'רטרס טיסות שכר מוזלות טיסה לחו"ל דילים Says:

    It was interesting

  6. בית המקדש , הכותל המערבי , כותל , הר הבית Says:

    This particular rails magic occurs in the routes

  7. list of best online casinos Says:

    One striking food cowered outside of the tired sort. The spatial best casino gambling won upon one encouraging best internet casinos. I chuckled that place beneath that best online casino sites. Some best gambling sites is statically existing. It’s principal to be got! Eh, some famous service affluently checked beyond one perfect best casinos. One best casinos is appallingly guilty. I smiled that best gambling sites save for one best online casino sites…

  8. השכרת רכב Says:

    del.icio.us indeed!

  9. Mugambar Says:

    Buy Viagra Online
    Buy Cialis Online
    Buy Propecia
    Buy Zithromax
    Buy Acomplia

  10. farmers Says:

    Nice Site! http://google.com

Leave a Reply