0

I am using friendly_id gem to have pretty urls in my application.I want an url like:

localhost:3000/posts/1/my-first-post

I want to do it using the friendly_id gem as i want to use the History module to avoid 404 error.I have tried but can't include the id in the slug.The code i am using is follows:

class Post < ActiveRecord::Base

    extend FriendlyId
    friendly_id :pretty_url, use: [:slugged, :history]

    def pretty_url
        "#{id}/#{title}"
    end
end

But it is not including the id in the slug.Rather it is using the slug as if only title is used.

ananyo2012
  • 63
  • 9

2 Answers2

1

The id of Post is set after the entry is saved. pretty_url doesn't work because the id doesn't exist yet.

You can get this behaviour without using the friendly_id gem by doing:

def to_param
  "#{id} #{title}".parameterize
end
BillyBib
  • 305
  • 4
  • 21
0

I think I remember reading somewhere that FriendlyId runs into issues with slashes within your slug function. Try "#{id}-#{title}" instead to see if that's the case.

Synergist
  • 517
  • 4
  • 20