Skip to content

Add a way to disable cached_slug in certain paths helpers #23

@DavidBennettPIO

Description

@DavidBennettPIO

Hi Guys,

I'm looking for a way to use the id instead of the slug in certain path helpers.

Namely I want admin_posts_path post to generate /admin/posts/2 and not /admin/posts/my-cool-post

So far I have come up with a ugly solution of overriding the to_param for each model in the admin controller and then back again..

class Admin::AdminController < ApplicationController

  before_action :dont_slug
  after_action :do_slug

  private

  def dont_slug

    Post.class_eval do
      def to_param
        self.id.to_s
      end
    end

  end

  def do_slug

    Post.class_eval do
      def to_param
        self.cached_slug.to_s
      end
    end

  end

end

But this is really ugly when you have 10 models using slugs.

I do know I could override each of the path helpers... but I have hundreds of paths and adding one for each route is prone to errors.

Any help with a better work-around would help me a lot!

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions