Reputation: 3
I have a problem with Controller Show Product. Can anyone help me out?
No route matches
{:action=>"show", :controller=>"products", :id=>nil, :vendor_id=>"3"}
missing required keys: [:id]
<td>
<%= link_to 'Show', vendor_product_path(current_vendor, @product) %><br>
<%= link_to 'Edit', edit_product_path(product) %><br>
<%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %>
</td>
my controller in product
def show
@product = Product.find(params[:vendor_id])
end
Upvotes: 0
Views: 47
Reputation: 1768
If you're trying to find all the products for a specific vendor, it would be something like:
def show
@vendor_products = Vendor.find(params[:vendor_id]).products
end
If you are just trying to find a single product by id:
def show
# Since :id is currently nil in params, this will not work
@product = Product.find(params[:id])
end
Upvotes: 1
Reputation: 3770
In the view, where are you getting @product
from? Seems like @product
is not yet saved to the database, and so it is nil in the view (in the line <%= link_to 'Show', vendor_product_path(current_vendor, @product) %>
. Which view file have you shown, and what is the corresponding controller action?
In that controller action, make sure you add a line to save the product @product.save
In case the @product is valid, then try passing in the fields explicitly:
<%= link_to 'Show', vendor_product_path(:vendor_id => current_vendor.id, :id => @product) %>
Upvotes: 0