设计更利于搜索的url是SEO的一个要点,我们来看看怎样将http://localhost/products/6转化成
http://localhost/products/6-gallon-of-milk或者http://localhost/products/gallon-of-milk
对第一种url,首先添加permalink字段:
- create_table "products", :force => true do |t|
- t.column "name", :string
- t.column "price", :float
- t.column "description", :text
- t.column "permalink", :string
- end
- <%= link_to h(product.name), :action => 'show', :id => product %>
- def to_param
- "#{id}-#{permalink}"
- end
- def show
- @product = Product.find(params[:id])
- end