简体   繁体   中英

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]

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. 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)

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:

<%= 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:

#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. To test this, manually insert a @category value:

<%= 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]) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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