Skip to content

SEO rubyonrails

September 13, 2011

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])
Advertisements

From → Ruby, Rubyonrails

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: