Lack of required keys

Rails rookie here. Attempting to link to the cart page shows an image from my navigator. the code:

<%= link_to(image_tag("cart.png"),cart_path) %> 

gives me this error:

No route matches {:action=>"show", :controller=>"carts"} missing required keys: [:id]

My cart controller starts with this:

def show
   begin
     @cart = Cart.find(params[:id]) 

Any understanding of your wizards would be wonderful.

+1
source share
4 answers

Since you say that you are a beginner, I will explain this in detail. This statement:

@cart = Cart.find(params[:id])

It means "look in the database and find the cart with the identifier X". However, when you do this:

<%= link_to(image_tag("cart.png"),cart_path) %> 

You do not provide this identifier. That is why it gives you this error.

I don't know how your routes are defined, but you can do something like this:

<%= link_to(image_tag("cart.png"),cart_path(cart_id_here) %>

What a way to say: "path to cart with identifier X".

+7

cart_path(my_cart_object).

, . cart_path(5), .

0

, id cart_path, .

<%= link_to(image_tag("cart.png"), cart_path(your_cart_id)) %> 
0

. , "cart_path (cart)", "" .

0

All Articles