簡體   English   中英

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

[英]How to override product_url in Spree 2.x / Rails 4 to make more SEO friendly?

我希望我的產品網址看起來像:

/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

現在的問題是,通過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

就是這樣。 現在,spree生成的所有鏈接都將使用該新的URL結構。 您可以修改本指南,在您的產品的spree中制作您想要的任何網址結構。


故障排除提示

不知道為什么,但是在我創建ProductsHelper之后,當我去購物車時遇到一個未定義的函數時出現了錯誤:line_item_description_text

我沒有在我的購物車中使用正常的狂歡描述,所以為了解決這個問題,我剛剛補充說:

def line_item_description_text (var)
  ""
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM