经过大量研究后我明白了。
此过程有两个步骤。首先是创建与新产品路线相匹配的路线。
因此,进入你的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
现在的问题是,spree 自动生成的所有产品页面链接仍将使用旧的 URL 结构,因此我们也必须修复该问题。为此,我们将为 spree 用于生成这些 url 的 Product_path 函数创建一个重写。在 helpers 文件夹中创建一个名为“spree”的新目录,然后在该目录中创建一个名为 products_helper.rb 的新文件
现在在这个文件中app/helpers/spree/products_helper.rb
添加以下代码:
module Spree::ProductsHelper
def product_path(product)
"/#{product.permalink}/p"
end
end
就是这样。现在,spree 生成的所有链接都将使用该新的 URL 结构。您可以修改本指南,为您的产品创建您想要的任何 url 结构。
故障排除提示:
不知道为什么,但在创建 ProductsHelper 后,当我进入购物车时收到一个关于未定义函数的错误:line_item_description_text
我不使用购物车中的正常狂欢描述,因此为了解决此问题,我刚刚添加了:
def line_item_description_text (var)
""
end