Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEO friendly URLs in RoR

Let's say I have a relatively basic CRUD application for editing music albums. In the database I have:

id | album_name | artist  |  navigation

1    Lets Talk   Lagwagon    lets-talk

However, instead of albums/1 returning my Show page, I want the page to be accessible by the albums/lets-talk route.

So in my controller I have:

  def show
    @album = Album.find_by_navigation(params[:id])
  end

And in my index view I have:

<%= link_to 'Show', :controller => "albums", :action => "show", :id => album.navigation %>

This successfully performs its function, however, the Ruby API says my link_to method is old and archaic without listing an alternative, so I suspect I'm going about this the wrong way.

like image 964
Nick Brown Avatar asked Nov 28 '22 16:11

Nick Brown


2 Answers

Define your to_param in the model:

class album
    def to_param
        "#{id}-#{album-name.parameterize}"
     end
end

Now you can use

<%= link_to album.album_name, album %>

to create the link to the seo path

Then in your controller:

Album.find(params[:id])

will call to_i -> so you get the Topic.find(2133) or whatever.

This will result in urls of the form: "/album/2-dark-side-of-the-moon" which although not exactly what was asked for has advantages - The id is used to find the album - which is unique, when the name might not be.

The parameterize method "Replaces special characters in a string so that it may be used as part of a ‘pretty’ URL" - http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize

like image 55
Will Avatar answered Dec 16 '22 10:12

Will


Check out friendly_id gem. It is exactly what you need.

like image 37
Sergey Kishenin Avatar answered Dec 16 '22 10:12

Sergey Kishenin