Search site

Add to Google Subscribe in NewsGator Online

Featured Articles
Search engine friendly link_to function in Rails back
Previous article: Real world usage of ActionMailer for account activation view
Next article: How to cache the output of an XML page in Rails view

Rails_logo

The link_to() function provided by the ActionView::Base class is used to generate a clickable anchor in your rhtml template. For example, let's say if you want to generate an anchor to an item with id 12, name "Hello World", via a controller method fetch_item, then the code will look like:

<%= link_to @item.name, { :action => 'fetch_item', :id => @item } %>

Which generates the following html:

<a href='/fetch_item/12'>Hello World</a>

If you modify the calling code a bit to include the whole path of your server http://www.mysite.com/ :

<%= link_to @item.name, { :action => 'fetch_item', :id => @item, :only_path => false } %>

Then the generated html will look like:

<a href='http://www.mysite.com/fetch_item/12'>Hello World</a>

This URL is much more search engine friendly. The other two benefits are:
  • if someone makes a copy of your page and put it on their server, all the traffic still go to your site when visitors click on something (and clicks are money these days, right ?). I am sure they can edit the page to remove all the absolute paths, but why make it easy for them ?
  • The absolute URL is fully compatible with SSL, meaning when you enable your site for e-commerce, you will not have to rework the pages. Otherwise, you will constantly get these annoying popups asking you about secure and insecure contents being on the same page

You can go even one step further and make the page you are serving look like a static html page, which is much more search engine, bookmark,  and Delicious friendly by pre-processing the URL for single pages to append it with a .html suffix (to make it look like a static page), before passing it to link_to:

<%= link_to @item.name, url_for(:action => 'fetch_item', :id => @item, :only_path => false) + ".html" %>

Which generates:

<a href='http://www.mysite.com/fetch_item/12.html'>Hello World</a>

I would really love to have an option to the link_to() method where I can specify this optional artificial suffix, maybe something like:

<%= link_to @item.name, { :action => 'fetch_item', :id => @item, :only_path => false}, :suffix => ".html" %>

or maybe even specify the the default suffix as an initialisation parameter to ActiveRecord::Base for pages which are passed an id. If anyone knows that this option exists, or will be supported soon, do let me know.

Please note that the last step to masquerade your single page URL's as static pages only work if you are running under Apache with URL rewriting turned on. You will have to check if the following lines exist in your public/.htaccess file:

RewriteRule ^([^.]+)$ $1.html [QSA]

By the way, you can also deduce from the above snippets that the easiest way to find out the URI of the page you are on programmatically is by calling:

url_for(:only_path => false)

 

back
Previous article: Real world usage of ActionMailer for account activation view
Next article: How to cache the output of an XML page in Rails view

discuss
 by by David at 23 Mar 2006 12:45:34
Copyrights © Transcraft Trading Limited 2006.All rights reserved. Bots Rss-rss