Monday, September 12, 2011

SEO rubyonrails

In your model override the to_param method. For instance
class Post < ActiveRecord::Base
def to_param
“#{id}-#{title.parameterize}.html”
end
end

Assume we have a post :
 @post = Post.create! :title => "simple SEO with rubyonrails",
:content => "blah blah ... "

In our view we call
link_to  "Detail", post_url(@post) # "1-simple-SEO-with-rubyonrails"

We get seo url pretty easy.

In our controller we simply call the find or where method as normal since Rails will convert the params[:id] to integer automatically :
@post = Post.where(["id=?", params[:id]) # or @post = Post.find(params[:id])

No comments:

Post a Comment