如何在Spree 2.x / Rails 4中覆盖product_url以使SEO更友好?

我希望我的产品url看起来像:

/product-name-here/p

代替:

/product/product-name-here

我怎样才能做到这一点?

经过大量的研究,我发现了它。

这个过程有两个步骤。 第一种是创建与新产品路线匹配的路线。

所以进入你的routes.rb和本节:

 mount Spree::Core::Engine, :at => '/' # Spree Specific routes here end 

添加以下行: get ':id/p' => 'spree/products#show'

所以现在它看起来像这样:

 mount Spree::Core::Engine, :at => '/' # Spree Specific routes here get ':id/p' => 'spree/products#show' end 

此时,您应该能够使用新的url结构访问产品页面: /product-name-here/p

现在的问题是,狂欢到产品页面自动生成的所有链接仍将使用旧的URL结构,因此我们也必须解决这个问题。 为此,我们将为spree用于生成这些URL的product_path函数创建一个覆盖。 在helper文件夹中创建一个名为“spree”的新目录,然后在该目录中创建一个名为products_helper.rb的新文件。

现在在这个文件app/helpers/spree/products_helper.rb添加以下代码:

 module Spree::ProductsHelper def product_path(product) "/#{product.permalink}/p" end end 

就是这样。 现在,spree生成的所有链接都将使用新的URL结构。 您可以修改本指南,在您的产品的spree中制作您想要的任何url结构。


故障排除提示

不知道为什么,但是在我创建ProductsHelper之后,当我去购物车时遇到一个未定义的函数时出现了错误:line_item_description_text

我没有在我的购物车中使用正常的狂欢描述,所以为了解决这个问题,我刚刚补充说:

 def line_item_description_text (var) "" end