friendly_id and routes.rb - rails

I'm currently using friendly_id gem in rails and noticed that if someone names a post "About" that it overwrites the /about path that I have assigned to a static page in my routes.rb file.

This is my current code:

  extend FriendlyId
  friendly_id :title, use: :history

If there are prior posts with the same adds a --2. But friendly_id seems to ignore static routes in my routes.rb.

Is there a way to make friendly_id recognize and not overwrite these routes?

Thank you


FriendlyID includes a Reserved module which prevents a list of provided words from being used as friendly slugs. You could add your static routes to the reserved words array which would prevent someone from overwriting your routes.

From the FriendlyId RDocs

FriendlyId.defaults do |config|
  config.use :reserved
  # Reserve words for English and Spanish URLs
  config.reserved_words = %w(new edit nueva nuevo editar)

If you still want to allow for a title that is reserved you can make a new method that FriendlyId would use for the slug. This piece from the RDocs explains that

Column or Method?

FriendlyId always uses a method as the basis of the slug text - not a column. It first glance, this may sound confusing, but remember that Active Record provides methods for each column in a model's associated table, and that's what FriendlyId uses.

Here's an example of a class that uses a custom method to generate the slug:

class Person < ActiveRecord::Base
  friendly_id :name_and_location
  def name_and_location
    "#{name} from #{location}"

bob = Person.create! :name => "Bob Smith", :location => "New York City"
bob.friendly_id #=> "bob-smith-from-new-york-city"

You could create a method like :title_with_id or :title_with_rand. it's up to you and how you'd like the slugs to look.

You would also want to make sure your routes.rb has your static routes listed prior to the routes for with the friendly id. The first route dispatcher matches is where the request will be processed.

Need Your Help

clearTimeout when a function gets called multiple times

javascript jquery

I have an alert message bar that slides down when the user clicks a specific link. The alert bar slides back up after a specified period. If the user clicks this link multiple times, I would like to

Is it possible to launch multiple EC2 instances within R?

r amazon-ec2

This is more of a beginner's question. Say I have the following code: