Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails如何仅在前端而不是在管理中使用友好id_Ruby On Rails_Ruby_Friendly Id - Fatal编程技术网

Ruby on rails rails如何仅在前端而不是在管理中使用友好id

Ruby on rails rails如何仅在前端而不是在管理中使用友好id,ruby-on-rails,ruby,friendly-id,Ruby On Rails,Ruby,Friendly Id,我只想在前端而不是在管理中使用友好的_id生成的slug。如何做到这一点 在我的管理控制器中: def find_data @product = Product.find(params[:id]) end def set_data @product = current_user.products.friendly.find(params[:id]) end 在我的前端控制器中: def find_data @product = Product.find(par

我只想在前端而不是在管理中使用友好的_id生成的slug。如何做到这一点

在我的管理控制器中:

def find_data
    @product = Product.find(params[:id])
  end
def set_data
    @product = current_user.products.friendly.find(params[:id])
  end
在我的前端控制器中:

def find_data
    @product = Product.find(params[:id])
  end
def set_data
    @product = current_user.products.friendly.find(params[:id])
  end
两个代码仍然返回相同的结果(使用友好的\u id生成的slug)

类产品
有人能帮忙吗?谢谢

更新


我在AciveAdmin中找到了这样做的方法。但是如果你不想使用友好的id slug,我就不会使用ActiveAdmin。只需在代码中使用以下内容:

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

而不是:

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


我希望它对任何与我有相同问题的人都有用

唯一的解决方案是不要重写
to_param
model方法,而是创建一个并行方法,在调用时输出slug

然后,在公共端需要的地方显式调用此方法。或者,您可以执行相反的操作,并将默认的
to_param
设置为友好id,并在生成路由时在管理部分显式传递id