我一直在抓我的头了这一点,但我不断收到此错误轨路由 - 路径问题
没有路由匹配{:动作=>“秀”,: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
'category_products_path(CATEGORY_ID:@Category)'需要注意的是在'复数products'。 – max
但链接到索引仍然没有多大意义。我认为你在寻找的是'link_to product.name,category_product_path(category_id:product.category,id:product)' – max
嘿。第一个不起作用,给出了同样的错误,然而第二个就像一个魅力。谢谢。是的,这没有多大意义,我还在学习。我想实际做的是进入类别页面,然后从中选择产品类别并列出它们。所以基本上这就是某种意义上的索引......或者如果你愿意的话,它可能只是一个'过滤器'页面。哦,我有一个问题,你可以向我解释这部分'category_id:product.category,id:产品'目前还不清楚,我无法在rails文档中找到任何有关多个参数的路径的信息 –