如何在 Spree 2.x/Rails 4 中覆盖 product_url 以使 SEO 更友好? [英] How to override product_url in Spree 2.x / Rails 4 to make more SEO friendly?

查看:29
本文介绍了如何在 Spree 2.x/Rails 4 中覆盖 product_url 以使 SEO 更友好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的产品网址如下所示:

I would like my product urls to look like:

/product-name-here/p

代替:

/product/product-name-here

我怎样才能做到这一点?

How can I achieve this?

推荐答案

经过大量研究我想通了.

After a lot of research I figured it out.

这个过程有两个步骤.首先是创建一条与新产品路线相匹配的路线.

There are two steps in this process. The first is to create a route that matches the new product route.

所以进入你的 routes.rb 和这个部分:

So go into your routes.rb and in this section:

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

添加这一行:get ':id/p' =>'狂欢/产品#show'

所以现在看起来像这样:

So now it looks like this:

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

此时您应该可以访问具有新 url 结构的产品页面:/product-name-here/p

At this point you should be able to visit the product page with the new url structure: /product-name-here/p

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

Problem now is that all of links automatically generated by spree to product pages will still use the old URL structure, so we must fix that as well. To do this we're going to create an over-ride for the product_path function that spree uses to generate these urls. Create a new directory in your helpers folder called "spree" and then a new file inside that directory called products_helper.rb

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

Now in this file app/helpers/spree/products_helper.rb add the following code:

module Spree::ProductsHelper

  def product_path(product)
    "/#{product.permalink}/p"
  end

end

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

And that's it. Now all of the links spree generates will use that new URL structure. You can modify this guide to make whatever url structure you want within spree for your products.

问题排查提示:

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

Not sure why, but after I created the ProductsHelper I was getting an error when I went to the cart about an undefined function: line_item_description_text

我没有在我的购物车中使用普通的狂欢描述,所以为了解决这个问题,我刚刚添加了:

I don't use the normal spree descriptions in my cart, so to fix this I just added:

def line_item_description_text (var)
  ""
end

这篇关于如何在 Spree 2.x/Rails 4 中覆盖 product_url 以使 SEO 更友好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆