Reputation: 181
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
Upvotes: 1
Views: 99
Reputation: 76784
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]
Upvotes: 1
Reputation: 15525
For a nested resource route, you have to pass the ids/objects of both the category and the product:
<%= link_to product.name, category_product_path([product.category_id, product.id]) %>
Upvotes: 0