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:
The pupose of this file is to allow us to create our own, custom URLs. Here's the one that I created:
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:
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:
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' |
1 2 3 4 |
map.connect 'saved/:keyword', :controller => 'get', :action => 'saved_query' |
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 |
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:
June 10th, 2007 at 07:21 PM 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...
June 10th, 2007 at 07:21 PM 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
September 12th, 2007 at 04:55 PM
Hi! ultram [url=http://www.networkingitalia.it/modules.php?name=Forums&file=viewtopic&p=26352&c-topic=ultram]ultram[/url] Waiting for you!
February 20th, 2008 at 05:39 AM
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.
April 1st, 2008 at 07:42 AM
It was interesting
April 3rd, 2008 at 12:54 PM
This particular rails magic occurs in the routes
May 1st, 2008 at 02:22 AM
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…
May 26th, 2008 at 01:06 AM
del.icio.us indeed!
June 8th, 2008 at 02:20 AM
Buy Viagra Online
Buy Cialis Online
Buy Propecia
Buy Zithromax
Buy Acomplia
June 19th, 2008 at 06:42 AM
Nice Site! http://google.com