1

I was trying to install gem FriendlyId. I am following railscast: 314-pretty-urls-with-friendlyid. It seems pretty easy to install. But not working for me.

Here are the steps I did:

added gem "friendly_id", "~> 4.0.9" in Gemfile

I then ran bundle install command

Then modified my product model to this:

class Product < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name

  attr_accessible :name, :product_code, :recipe, :selling_price, :servings,   letsrate_rateable "quality", "packaging", "hygiene" , "service", "price"
  validates :name, :presence => true
  class << self
    def search(params)
             if params[:search]
                products = Product.published.includes(:product_sub_categories)
             end
    end
  end
 end

Still it is showing 'id':

http://localhost:3000/products/9

As per railscasts it should show product name. But it is not. I individually install gem but no effect.

Can anybody tell what I am missing?

sites
  • 21,417
  • 17
  • 87
  • 146
user2206724
  • 1,265
  • 3
  • 20
  • 38

1 Answers1

0

Not sure why this is not mentioned in Guide, but try:

alias_attribute :to_param, :slug

This works because to_param method allows to customise the id part of URL. And here you are assigning that part to friendly_id generated URL.

Another solution

I am not sure if this is implemented inside friendly_id, but I did this:

class Product < ActiveRecord::Base
  def self.find(id_or_slug)
    case id_or_slug
    when String
      find_by_slug id_or_slug
    else
      super id_or_slug
    end
  end
end
sites
  • 21,417
  • 17
  • 87
  • 146