简体   繁体   English

Rails路由-路径问题

[英]rails routing - path issue

i've been scratching my head over this, but i keep getting this error 我一直在为此挠头,但我一直收到这个错误

No route matches {:action=>"show", :category_id=>nil, :controller=>"products", :id=>nil} missing required keys: [:category_id, :id] 没有路由匹配{:action =>“ show”,:category_id => nil,:controller =>“ products”,:id => nil}缺少必需的键:[:category_id,:id]

In my simple minded logic whenever i go for category_product_path and pass in the category, i should have the list of products within that category. 按照我头脑简单的逻辑,每当我进入category_product_path并传递类别时,我都应该具有该类别内的产品列表。 But i'm probably missing something. 但是我可能会丢失一些东西。 This is my view file ... where i've tried several stuff without success 这是我的视图文件...在这里我尝试了几种方法而没有成功

<% @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 %>

This is my routes 这是我的路线

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]

And what i assume to be the problem, somewhere in the controllers (not the ones in the admin folder) 我认为是问题所在,在控制器中的某个位置(而不是admin文件夹中的那些)

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

You'll need to add both @category and product variables to your route: 您需要将@categoryproduct变量都添加到您的路线中:

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

-- -

Update 更新

I don't know where you're calling your view, but if I'm right in thinking it's categories/:category_id/products/ , then @category isn't being set: 我不知道您要在何处调用视图,但是如果我正确地认为它是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 

If you're not doing this, it does suggest your @category value is not present. 如果您这样做,则表明您的@category值不存在。 To test this, manually insert a @category value: 要对此进行测试,请手动插入@category值:

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

Tip: 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]

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM