Archive for July, 2006

Make ids in URL Search engine friendly

Generally in rails url are in form of :controller/:action:/:id for example post/view/9 .

URLs are considered extremely valuable. Not only because users have to see them all the time, but also because search engines give them a lot of weight: since it’s a “limited resource” where you can only include a few keywords, you better use the keywords that matter most.

We can use both post id and post title in the url to make them more search engine friendly as post/view/9-this-is-the-post-title.

This is simple, as rails treats :id as a special parameter in routes. It’s specialness comes from the fact that it would try to call the to_param method on any object passed when creating URLs. That’s why url_for :id => @post is equivalent to url_for :id => @post.id because ActiveRecord model’s have a default to_param that returns the id of the object.

All you need to do is define your own to_param for your models, and make sure you don’t explicitly include the .id in your url_for and link_to, because then you would be skipping your own to_param call.


  class Post < ActiveRecord::Base
    def to_param
      "#{id}-#{full_name.gsub(/[^a-z1-9]+/i, '-')}"
    end
  end

You can change -(hyphen) in gsub in to_param method by _, + or anything you wish.

July 26, 2006 at 10:32 am 1 comment

Plugin: validate_request

A very useful plugin by Scott A. Woods.

validate_request plugin allows us to check the request method and parameters that are used to call your action.

For Example consider an add_to_cart action as:


  def add_to_cart
    @product = Product.find(params[:id])
    @cart.add_product(@product)
  end

The link to add an item to our cart should like store/add_to_cart/nnn, where nnn is an integer. There will be an error if some one intentionaly enter store/add_to_cart/some_string_here.
ValidateRequest allows us to double check these things, and act appropriately. For instance, we could solve the above problem by adding one line to our action:


  def add_to_cart
    validate_request(:get, :id => :integer) or return
    @product = Product.find(params[:id])
    @cart.add_product(@product)
  end

The validate_request method will now verify that incoming requests are via the GET method, and that they contain one argument called ‘id’ whose value is an integer. If any of these conditions aren’t true, the requester is redirected to the site’s home page (configurable, of course), and flash[:error] is set with a polite message (also configurable).

Install the plugin by running the following commands from your rails application’s directory:

./script/plugin source svn://rubyforge.org//var/svn/validaterequest/plugins ./script/plugin install validate_request

For more details visit http://rubyforge.org/projects/validaterequest/

July 26, 2006 at 5:28 am Leave a comment

Plugin: tabnav

Add tabbed navigation to your Rails application in a easiest way.

For more details please visit http://blog.seesaw.it/articles/2006/07/23/the-easiest-way-to-add-tabbed-navigation-to-your-rails-app

July 24, 2006 at 5:28 am 2 comments

An Escalator Puzzle

Bob takes the underground train to work and uses an escalator at the railway station. If Bob runs up 6 steps of the escalator, then it takes him 52.5 seconds to reach the top of the escalator. If he runs up 14 steps of the escalator, then it takes him only 32.5 seconds to reach the top.

How many seconds would it take Bob to reach the top if he did not run up any steps of the escalator at all?

July 22, 2006 at 6:34 am 1 comment

JotSpot.com

JotSpot Live allows you, your colleagues or clients to take notes together on the same web page at the same time. Imagine everyone simultaneously typing and editing the same Microsoft Word document and you’ll get the idea. Visit JotSpot.com

July 14, 2006 at 5:39 am 1 comment

19 Rails Tricks Most Rails Coders Don’t Know

I was searching for some Rails tips and found a very good post, Click here to visit

July 10, 2006 at 6:41 am Leave a comment


Important Notice…

I have moved this blog to http://webonrails.com . I will continue posting at http://webonrails.com, not on http://webonrails.wordpress.com.

Kindly update your book marks to http://webonrails.com

and http://webonrails.com/feed .

I am really very sorry for inconvenience.

Comming Soon….

Domain for Sale

TheRailsHosting.com


(Contact me)

Recent Posts

Calender

July 2006
M T W T F S S
« Jun   Aug »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Subscribe

  • Add to Google
  • Subscribe in NewsGator Online
  • Subscribe with Bloglines
  • WebOnRails on feedburner
  • technorati

del.icio.us

Feeds

Flickr Photos

Dunnocklet!

To Let

Coquet Orange

More Photos

Visitor’s Location

Blog Stats

  • 16,049 hits

Follow

Get every new post delivered to your Inbox.