0

I use gem 'friendly_id', '~> 5.1.0' but not any effect on my id.

this is my work:

model:

extend FriendlyId
friendly_id :company_name, use: :slugged

Controller:

@company = Company.friendly_id.find(params[:company_id])

View:

<%= company_path(:company_id => c.company_id) %>

Routes

get  'company/:company_id'   => 'company#company', as: :company

Where is my problem?

I use also slug into the database table

1 Answers1

0

Seems like you are adding friendly id to an existing model. That's why previously saved values does not show a friendly_id in url.

If this the case you need to run following on console.

Company.find_each(&:save)

This will generate friendly_id for all existing record. For future record gem will work as expected.

You are having another issue, Due to that you are not getting this issue. You are passing id in routes, Not company object. That's why friendly id not getting any slug while creating url for you.

In your view I am assuming @company is object for company that you want to visit. Now change url in your view like below.

<%= company_path(@company) %>

This will work for you.

Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32