3

I have a Rails application that uses the friendly_id Gem.

Using Friendly ID works well.

However, there are some paths in the backoffice where I do not want to use the Friendly ID slug in the URI, as it is not possible to use the same scopes as in the front-end.

e.g. I have an article that can have multiple versions. On each blog on the front, another article version is shown. Therefore, I can use @blog.articles.friendly.find(params[:id]). However, in the backoffice, I have pages where people can edit such a version, which is not nested under a Blog (because it is possible for a version to change blog using the form). Therefore, article versions that have the same slug as an earlier one will become inaccessible.

How can I instruct friendly_id and/or the Rails URL helper to not use the slug for these paths, but just use the good old numerical ID instead?

Qqwy
  • 5,214
  • 5
  • 42
  • 83

1 Answers1

5

In the Article model, add finders options to friendly_id attribute

class Article < ActiveRecord::Base
  ...
  friendly_id :name_of_article, :use => [:slugged, :finders]
  ...
end

In this case, whatever the params[:id] is, numerical id or slugs, you should still be able to get the article by Article.find(params[:id]).

Not sure if I understood your question correctly. Hope it helped!

Vic
  • 1,512
  • 17
  • 25
  • The problem is, that at some point there might be two Articles with the same slug. Which is expected behaviour, because they are scoped on `Blog`. e.g. `friendly_id :name_of_article, use: [:slugged, :history, :scoped], scope: :blog`. However, my articles on the back-end are not scoped on blog, so I would like to keep using the numerical IDs there. – Qqwy Feb 21 '16 at 13:15