缺少必需的键 [英] Missing required keys
问题描述
Rails 菜鸟在这里.尝试从导航栏中的图像链接到购物车显示页面.代码:
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) %>
给我这个错误:
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.
推荐答案
既然你说你是菜鸟,那我就详细解释一下.本声明:
Since you say you're a rookie, I'll explain it in detail. This statement:
@cart = Cart.find(params[:id])
表示在数据库中查找并找到 ID 为 X 的购物车".但是,当您这样做时:
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) %>
您没有提供该 ID.这就是为什么它会给你那个错误.
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) %>
这是一种说法,ID 为 X 的购物车的路径".
Which is a way of saying, "the path to the cart with id of X".
这篇关于缺少必需的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!