【问题标题】:rails routing - path issue导轨路由 - 路径问题
【发布时间】:2015-10-25 10:14:01
【问题描述】:

我一直在为这个问题摸不着头脑,但我一直收到这个错误

没有路线匹配 {:action=>"show", :category_id=>nil, :controller=>"products", :id=>nil} 缺少必需的键: [:category_id, :id]

在我的简单逻辑中,每当我选择 category_product_path 并传入类别时,我都应该拥有该类别中的产品列表。但我可能错过了一些东西。这是我的视图文件...我尝试了几个没有成功的东西

<% @products.each do |product| %>
    <tr>
        <td><%= link_to product.name, category_product_path(@category) %></td>
        <td><%= product.category_id %></td>
        <td><%= number_to_euro(product.price) %></td>
        <td><%= product.stock %></td>
        <td><%= image_tag(product.image.thumb) %></td>
        <br>
        </tr>
<% end %>

这是我的路线

namespace :admin do
  resources :categories
  resources :products
  resources :orders
end
resources :categories, only: [:index, :show] do
  resources :products, only: [:index, :show]
end
resources :orders, only: [:new, :create]

而我认为是控制器中某处的问题(而不是管理文件夹中的那些)

class CategoriesController < ApplicationController    
    before_action :set_category, only: [:show]
    def index
      @categories = Category.all
    end

    def show
      @products = @category.products
    end

    private

    def set_category
      @category = Category.find(params[:id])
    end
end

class ProductsController < ApplicationController
    before_action :set_product, only:[:show]
    def index
        @products = Product.all
    end

    def show
    end

    private

    def set_product
      @product = Product.find(params[:id])
    end
end

【问题讨论】:

  • category_products_path(category_id: @category) 请注意,products 是复数形式。
  • 但是链接索引还是没有多大意义。我认为您正在寻找的是link_to product.name, category_product_path(category_id: product.category, id: product)
  • 嘿。第一个不起作用,给出了同样的错误,但是第二个就像一个魅力。谢谢你。是的,这没有多大意义,我还在学习。我真正想做的是转到类别页面,然后从那里选择产品类别并列出它们。所以基本上这在某种意义上是索引......或者如果你愿意的话,它可能只是一个“过滤器”页面:)。哦,我有一个问题,你能向我解释一下这部分'category_id:product.category,id:product'吗?不清楚,我在rails文档中找不到关于具有多个参数的路径的任何内容

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您需要将 @categoryproduct 变量添加到您的路线中:

<%= link_to product.name, category_product_path(@category, product) %>

--

更新

我不知道你在哪里调用你的视图,但如果我认为它是 categories/:category_id/products/ 是正确的,那么 @category 没有被设置:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def index
      @category = Category.find params[:category_id]
      @products = @category.products
   end
end 

如果您这样做,则表明您的@category 值不存在。要对此进行测试,请手动插入 @category 值:

<%= link_to product.name, category_product_path("2", product) %>

提示:Multiple resource declarations

#config/routes.rb
namespace :admin do
  #needs a root
  resources :categories, :products, :orders
end
resources :categories, only: [:index, :show] do
  resources :products, only: [:index, :show]
end
resources :orders, only: [:new, :create]

【讨论】:

  • 感谢您的提示,我需要阅读更多有关此(路线)的信息并将其放在一起。但是提供的解决方案导致'没有路线匹配 {:action=>"show", :category_id=>nil, :controller=>"products", :id=>"2"} 缺少必需的键:[:category_id]' .这对我来说越来越不清楚,zwippie 发布了类似的内容,我知道两者都需要通过。但是考虑到@category 不起作用=> 我认为这是主要原因,是不是我需要在 products_controller 中定义它?就像我在类别中定义产品一样?这是我最初的想法
  • 是的,就是这样!哈,我想我的想法是正确的,但只是不知道如何将其付诸实践……谢谢!然而,对于一个问题,max 早些时候说了一些关于它的事情。在正常的应用程序中,您很少会看到产品索引,通常您会看到基于类别的产品。在这种情况下,为函数/视图使用不同的名称是否合乎逻辑?比如... category_list 什么的?
  • 不,你做得很好——你会看到网址为http://url.com/categories/1/products,这对我来说似乎是合乎逻辑的
【解决方案2】:

对于嵌套资源路由,您必须同时传递类别和产品的 ID/对象:

<%= link_to product.name, category_product_path([product.category_id, product.id]) %>

【讨论】:

  • 嗯好的,我学到了一些新东西,我需要通过它们。但是此解决方案会引发错误 - 没有路由匹配 {:action=>"show", :category_id=>[1, 2], :controller=>"products", :id=>nil} 缺少必需的键:[: id]
猜你喜欢
  • 1970-01-01
  • 2010-11-22
  • 2011-03-29
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 2015-08-14
相关资源
最近更新 更多