Kris MP
Kris MP

Reputation: 2415

rails how to use friendly_id only in frontend not in admin

I only want to use friendly_id generated slug in frontend not in admin. How to accomplished this?

in my admin controller:

def find_data
    @product = Product.find(params[:id])
  end

in my frontend controller:

def set_data
    @product = current_user.products.friendly.find(params[:id])
  end

Both code still return the same result (using friendly_id generated slug)

class Product < ActiveRecord::Base

extend FriendlyId

friendly_id :name, use: [:slugged, :finders, :history]

private

def should_generate_new_friendly_id?
    new_record? || slug.blank? || name_changed?
  end

anybody can help? thanks

UPDATE

I found the way to do this in AciveAdmin here. But Im not using ActiveAdmin

Upvotes: 2

Views: 596

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176402

The only solution is to not override the to_param model method, but instead create a parallel method that outputs the slug when called.

Then, explicitly call this method where you need it on the public side. Or you can do the opposite, and default to_param to the friendly id and explicitly pass the ID in the admin section when generating a route.

Upvotes: 2

Kris MP
Kris MP

Reputation: 2415

If you don't want to use friendly_id slug. Just use this in your code:

<%= link_to(product.name, admin_product_path(product.id)) %>

instead of:

<%= link_to(product.name, admin_product_path(product)) %>

I hope it will be useful for anyone who had same question with me

Upvotes: 0

Related Questions