【问题标题】:Create canonical urls in rails /:category/:product/:item/ in rails 4在 rails 4 中的 rails /:category/:product/:item/ 创建规范 url
【发布时间】:2015-12-21 12:32:49
【问题描述】:

如何通过link_to 方法以/:category/:product/:item/ 的形式创建一个url。

我试过这个:

parameters: {category: @category, product: @product, item: @item}

= link_to "list", url_for(controller: "items", action: "list", params: parameters), 
  class: "button radius #{ additional_classes }", rel: "canonical"

但它给了我这个网址:/list?product=a&category=b&item=c。我真的很想获得其他格式

PS:我用的是 slim 但不是HTML

【问题讨论】:

  • 能否请您展示您的路线文件您是如何使用资源的?

标签: ruby-on-rails routes


【解决方案1】:

我设法通过合并解决了这个问题:

parameters: {category: @category, product: @product, item: @item}

= link_to "list", url_for(controller: "items", action: "list").merge(parameters), class: "button radius #{ additional_classes }", rel: "canonical"

【讨论】:

    【解决方案2】:

    这样做:

    #config/routes.rb
    resources :categories, path: "" do
       resources :product, path: "" do
         resources :item, path: ""
       end
    end
    

    这将创建以下网址:url.com/:category_id/:product_id/:id

    您将能够使用以下link_to 助手:

    <%= link_to @item.title, [@category, @product, @item] %>
    

    --

    虽然它应该可以工作,但它是not recommended

    资源的嵌套深度不得超过 1 级。

    因此,您可能希望重构您的路由/基础架构,以便首先调用 item_id -- productcategories 作为索引方法:

    #config/routes.rb
    resources :categories, path: "", only: :index
    resources :products, only: :index do
       resources :items
    end
    

    这将允许您致电 &lt;%= link_to "Products", products_path %&gt; 获取产品列表,然后调用 &lt;%= link_to @item.id, products_item_path(@product, @item) %&gt; 获取特定商品。


    你也不妨看看friendly_id

    这不会对您的路线产生任何影响(它位于您的模型中)——它基本上允许您使用 slugs 来识别您的资源(而不是 id 的)。

    如果您想了解更多信息,我可以更新答案;基本上它会允许你有这样的网址:

    url.com/:category_id/:product_id/:item
    url.com/category-name/product-name/item-name 
    

    友好 ID

    Friendly ID 只是用slug 参数替换了你的路由/find 方法的:id 参数。要设置friendly_id,您基本上需要在模型中添加slug 属性(通过迁移),然后在模型本身上包含FriendlyID 类。

    你是如何设置它的:

    # config/routes.rb
    resources :categories, path: "" do
      resources :subcategories, path: "" do
        resources :products, path: "" # => http://url.com/:category_id/:subcategory_id/:product_id
      end
    end
    

    需要注意的是,嵌套资源路由的深度是not recommended(我会留下来演示如何做)。

    Rails 将采用上述方法并创建一个路线助手,例如category_subcategory_product_path(:category, :subcategory, :product)

    通过为每个值传递 objects,“裸”Rails 将仅从它们推断出idFriendlyID(如果设置正确)应在所有这些模型中将 id 替换为 slug 列。

    你需要像下面这样设置它:

    # app/models/category.rb
    class Category < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name, use: [:finders, :slugged]
    end
    
    # app/models/subcategory.rb
    class Subcategory < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name, use: [:finders, :slugged]
    end
    
    # app/models/product.rb
    class Product < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name, use: [:finders, :slugged]
    end
    

    这 - 如果您迁移每个模型以包含 slug 列 - 将允许 Rails 使用 slug 对象填充路由助手。当然,如果你只是使用裸值也没关系:

    category_subcategory_product_path(@category.slug, @subcat.slug, @product.slug)
    

    诀窍是处理控制器中的 slug。

    你可以通过路由助手传递任何你想要的值(没有验证)。不同之处在于您如何管理控制器中传递的数据:

    # app/controllers/products_controller.rb
    class ProductsController < ApplicationController
      def show
        @product = Product.find params[:id] #-> FriendlyID will automatically look up the slug if present
      end
    end
    

    【讨论】:

    • 嗨,理查德,是否可以更新您关于friendly_id 部分的答案?这正是我需要的——这样的网址:url.com/category/subcategory/product-name——我还不知道该怎么做。
    • 好的,让我来做
    • 亲爱的理查德,我还在为这个问题苦苦挣扎。如果你能帮助我和其他人,你会非常好。
    • 对不起,当然是——我完全忘记了!我会在一秒钟内完成它
    • 你好。您的解决方案运行良好,再次感谢您更新帖子。这只是......有点奇怪,因为例如:-@categories.each do |category| =link_to category.title, category_path(category.slug) 由 INDEX 操作处理(并且categories_path 重定向到“/”)而不是 SHOW。我知道,它来自path: "/" 选项,但它有点奇怪。
    【解决方案3】:

    尝试像这样更改您的路线:

    get '/:category/:product/:item/', 'items#list', :as => 'item_list'
    

    所以你会得到item_list_path,你可以在这样的链接中使用它。

    = link_to "list", item_list_path(@category, @product, @item)
    

    但请确保传递参数的顺序与它们在 url 中的顺序相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      相关资源
      最近更新 更多