2015-10-25 264 views
1

我一直在抓我的头了这一点,但我不断收到此错误轨路由 - 路径问题

没有路由匹配{:动作=>“秀”,:CATEGORY_ID =>零, :控制器=> “产品”,:ID =>零}缺少必需的键: [: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 
+1

'category_products_path(CATEGORY_ID:@Category)'需要注意的是在'复数products'。 – max

+0

但链接到索引仍然没有多大意义。我认为你在寻找的是'link_to product.name,category_product_path(category_id:product.category,id:product)' – max

+0

嘿。第一个不起作用,给出了同样的错误,然而第二个就像一个魅力。谢谢。是的,这没有多大意义,我还在学习。我想实际做的是进入类别页面,然后从中选择产品类别并列出它们。所以基本上这就是某种意义上的索引......或者如果你愿意的话,它可能只是一个'过滤器'页面。哦,我有一个问题,你可以向我解释这部分'category_id:product.category,id:产品'目前还不清楚,我无法在rails文档中找到任何有关多个参数的路径的信息 –

回答

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] 
+0

谢谢你的提示,我需要阅读更多关于这(路线),并把它放在一起。然而,提供的解决方案导致“无路由匹配{:action =>”show“,:category_id => nil,:controller =>”products“,:id =>”2“}缺少必需的键:[:category_id]' 。而这对我来说变得不清楚,zwippie发布了类似的东西,并且我得到了两个都需要通过的事实。但给@category不起作用=>我认为这是主要原因,可能是我需要在products_controller中定义它?就像它我定义的产品类别?这是我最初的想法 –

+0

哈哈哈也许答案很简单,让我更新 –

+0

是的,就是这样!哈,我想我有正确的想法,但只是不知道如何付诸实践......谢谢!然而,有一个问题,马克斯早些时候说过。在正常的应用程序中,您很少看到产品索引,通常您会看到基于类别的产品。在这种情况下,为函数/视图使用不同的名称是合乎逻辑的吗?像... category_list什么的? –

0

对于嵌套资源的路线,你必须同时通过类别和产品的IDS /对象:

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

hmm ok,我学到了一些新东西,我需要把它们都传递出去。然而这个解决方案带来了错误 - 没有路由匹配{:action =>“show”,:category_id => [1,2],:controller =>“products”,:id => nil}缺少必需的键:[: ID] –