Building a website with SEO considerations leads to have slugs in the urls.

There is the trivial case of /products/256-my-great-product easily done 
changing Product#to_param. But there many others cases not as trivial.

Considering the route (i use a plain match-like route instead of resourcesfor 
the example) :

get "/products/:type_slug/:brand_slug/:token-:slug", :to => 
"controller#action", :as => "product"

*I'd add that the id is in fact a md5-like id (NoSQL : mongodb, 
couchdb....), so we want naturally use a short token.*

Obviously, link_to("My product", @myproduct), url_for(@myproduct) and 
product_path(@myproduct) fail.

To generate such a route, we need to do :
 
product_path(:type_slug => product.type_slug, :brand_slug => 
product.brand_slug, :token => product.token, :slug => product.slug)
 

 So here is the thing. Doing link_to("My product", @myproduct), 
url_for(@myproduct) and product_path(@myproduct) is nice.

If we manually define :
 
class Product
  def to_params
    {
      :token => token,
      :slug => slug,
      :brand_slug => brand_slug,
      :type_slug => type_slug
    }
  endend
 

 Then we could use product_path(@myproduct.to_params).

And if the named url helpers recognize that our object respond_to? 
:to_params, using this #to_params-returned Hash to build the url, we could 
use product_path(@myproduct). For now, a named url helper is just basically 
replacing the dynamic url segments from left to right by its arguments from 
first to last, applying them a #to_param.

And if the polymorphic_url recognize that our object respond_to? :to_params, 
passing this #to_params-returned Hash to the named url helper, we could use 
link_to("My 
product", @myproduct), url_for(@myproduct).

Just enough to be nice. And SEO-friendly without pain.

Waiting for comments, thoughts and anything else helpful :)

Maxime.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-core/-/b38QotnRQF8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to