elersong
elersong

Reputation: 857

Why is Friendly_ID gem using slug instead of primary key?

I'm getting an error from rails after installing Friendly_ID because the code is misinterpreting the input, and I don't know how to fix the problem. In order for the MVC framework to understand the url and route it accordingly, the url needs to fit the following pattern:

localhost:3000/posts/[:id]

Above, the [id] represents the primary key for any object found in the Post model class. The misinterpretation comes into effect when Friendly_ID changes the url to fit the following pattern:

localhost:3000/posts/[:slug]

The updated url is obviously easier to read by the user, but since my application expects to get an [:id], it raises the following exception for the url "localhost:3000/posts/testing":

Rails exception screenshot


I had assumed that the Friendly_ID gem would either take care of the routing issue internally, perform some kind of [:id] lookup, or work its magic through some kind of url-masking. Since none of the above are true in this case, how to I resolve the issue, allowing the pretty-urls to deliver the same result as the original url pattern?

Upvotes: 0

Views: 499

Answers (2)

makhan
makhan

Reputation: 4009

Add config.use :finders to your config/initializers/friendly_id.rb

Here's what mine looks like:

FriendlyId.defaults do |config|
  config.use :reserved

  config.reserved_words = %w(new edit index session login logout users admin
    stylesheets assets javascripts images)

  config.use :finders
end

Upvotes: 0

urmurmur
urmurmur

Reputation: 232

Finder overriding is disabled since gem version 5.0

Use Post.friendly.find(params[:id]) or downgrade, or use finders addon, see project github page

Upvotes: 1

Related Questions