SEO Friendly RoR URLs

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/1returning my page Show, I want the page to be accessible along the route albums/lets-talk.

So, in my controller, I:

  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 succeeds in its function, however the Ruby API says my link_to method is old and archaic with no alternatives, so I suspect I'm wrong.

+4
source share
4 answers

Take a look at friendly_id gem. This is exactly what you need.

+4
source

to_param :

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

<%= link_to album.album_name, album %>

seo

:

Album.find(params[:id])

to_i โ†’ , Topic.find(2133) - .

URL- : "/album/2-dark-side-of-the-moon", , , , . - , .

" , " URL "- http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize

+16

URL- app/albums/lets-talk, POST album_id => 1, ..

:

# PRODUCT MODEL
 before_create do
   self.navigation = album_name.downcase.gsub(" ", "-")  
  end

def to_param
    [id, album_name.parameterize].join("-")
end

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

album = Album.find(params[:album_id])
+1
source

It seems that you want to override the id: to: name parameter. This case is described in this article .

class User < ActiveRecord::Base
  def to_param  # overridden
    name
  end
end

user = User.find_by_name('Phusion')
link_to 'Show', user_path(user)  # => <a href="/users/Phusion">Show</a>
0
source

All Articles