1

Rails rookie here. Attempting to link to a carts show page from an image in my navbar. 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 carts controller starts with this:

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

Any insight from you wizards would be great.

Alb
  • 221
  • 2
  • 5
  • 14
  • Are you writing an ecommerce website? Why not use Spree commerce, etc? – Mike Szyndel Aug 11 '14 at 20:11
  • 1
    @MichalSzyndel You want to make him suicide? Read his first sentence: "Rails rookie here". It would be good to know how things work before coding spree app... it is like writing all CRUD routes by your own before discovering `resources` method ;) – Filip Bartuzi Aug 11 '14 at 20:16
  • @MichalSzyndel I am trying to build it all without templates so I'm forced to F up, and really learn. I gave Spree a shot earlier and was overwhelmed with a bunch of stuff I did not understand. – Alb Aug 11 '14 at 20:19
  • Haha @FilipBartuzi precisely. – Alb Aug 11 '14 at 20:19
  • However spree's code is a really impressive piece of code. If you are wondering how to implement functionality to e-commerce in project you write from scratches it is good idea to have a look how spree team implemented this. – Filip Bartuzi Aug 11 '14 at 20:23
  • @FilipBartuzi I was hearing this "good to know how things work" BS from PHP programmers all the time and you know what? It's not a way to go. To learn something you have to have an idea what you're doing. It's better to learn to understand a well designed system that try to invent a wheel on your own (especially when this wheel turns out to be a supersonic plane) – Mike Szyndel Aug 11 '14 at 20:23
  • @FilipBartuzi And if OP doesn't understand why he should not access cart via id (but rather store cart id in session to make it safer) then let's now pretend that one SO question can get him to being able to write an ecommerce software. – Mike Szyndel Aug 11 '14 at 20:26
  • @alex don't get me wrong because it's great that you're curious and ambitious but really, you will profit more from learning incrementally and setting more reasonable goals for yourself :) – Mike Szyndel Aug 11 '14 at 20:27
  • @MichalSzyndel Can't fully agree with you. You're right that it's better to learn to understand a well designed system but without foundations like thing alex asked here you are not able to understand how huge framework like spree works. Of course alex could rush to Spree but he wouldn't learn how spree works - he would just learn how to use spree and eventually develop new things to it. Same thing with Rails. Even if you would be able to do everything in rails, without knowledge how rails works in backend (so it is tough ruby stuff) you would never be able to write your own framework. – Filip Bartuzi Aug 11 '14 at 20:31
  • Frameworks like spree solves tons of things so you don't have to face them. It is most likely alex would NOT discover best solution to passing cart_id inside e-commerce app because it is already solved in spree. The same thing happens in Rails. You literally DO NOT have to know ANY object oriented design pattern in order to create good working, nice apps but... could you imagine writing Rails without knowing these patterns? It is impossible. @MichalSzyndel – Filip Bartuzi Aug 11 '14 at 20:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59127/discussion-between-filip-bartuzi-and-michal-szyndel). – Filip Bartuzi Aug 11 '14 at 20:45
  • @MichalSzyndel Thank you Michal, and Filip. I'm creating the app in conjunction with a couple of tutorial books, which helps. I am indeed in over my head, by design. Thanks to all answerers. – Alb Aug 12 '14 at 02:07

4 Answers4

7

Since you say you're a rookie, I'll explain it in detail. This statement:

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

Means "look in the database and find the cart with an id of X". However, when you do:

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

You aren't providing that id. That's why it gives you that 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) %>

Which is a way of saying, "the path to the cart with id of X".

Ege Ersoz
  • 6,461
  • 8
  • 34
  • 53
  • Thanks for the explanation, this helped. But what is the actual code that replaces "(cart_id_here)"? – Alb Aug 11 '14 at 20:55
  • That will be your cart object or its id. Because you're asking the app to take you to the cart_path for a specific cart. This might be a cart that belongs to a user, in which case you might do `cart_path(current_user.cart)` for example. Hard to say without seeing how your models are set up. – Ege Ersoz Aug 11 '14 at 21:42
0

You need cart_path(my_cart_object).

The plain old id might work too, e.g. cart_path(5), but I could be mistaken.

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
  • How do I make that parenthetical touch on the current cart id? When I type in a random number for id the link doesn't break, but leads me to an "empty cart" show. – Alb Aug 11 '14 at 20:46
  • Not sure if I understand your question. You either have the id or you don't. If you don't have it when you need it, then you messed up. :) I don't know how you created the cart initially, but it sounds like you need to persist it somewhere. Maybe the session hash for your user would be a good place? Or if your user is logged in, maybe you'd create a User<->Cart association. – Grant Birchmeier Aug 12 '14 at 18:05
0

As there error is pretty descriptive you need to pass id to your cart_path as it's a required argument.

<%= link_to(image_tag("cart.png"), cart_path(your_cart_id)) %> 
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
0

You need to specify the cart. If you are looping through your carts, then you need to use "cart_path(cart)" where "cart" is the placeholder for the current cart.

jreinecke
  • 26
  • 3