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

new*_*ere 4 ruby-on-rails spree ruby-on-rails-4

我希望我的产品网址看起来像:

/product-name-here/p

代替:

/product/product-name-here

我怎样才能做到这一点?

new*_*ere 5

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

这个过程分为两步.第一种是创建与新产品路线匹配的路线.

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

mount Spree::Core::Engine, :at => '/' 
 # Spree Specific routes here
end
Run Code Online (Sandbox Code Playgroud)

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

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

mount Spree::Core::Engine, :at => '/' 
 # Spree Specific routes here
 get ':id/p' => 'spree/products#show'
end
Run Code Online (Sandbox Code Playgroud)

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

现在的问题是,通过spree自动生成到产品页面的所有链接仍将使用旧的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
Run Code Online (Sandbox Code Playgroud)

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


故障排除提示

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

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

def line_item_description_text (var)
  ""
end
Run Code Online (Sandbox Code Playgroud)