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







Recent Comments