我正在嘗試爲我的物品添加「添加到購物車」方法。沒有路線匹配[GET]
items_controller:
def to_cart
@item = Item.friendly.find(params[:id])
@item.add_to_cart
redirect_to root_path
end
路線:
resources :items do
put :to_cart, on: :member
end
型號:
def add_to_cart
current_user.cart.items << self
current_user.cart.save
end
顯示:
<%= @item.name %>
<%= link_to 'add to cart', to_cart_item_path(@item) %>
我得到了RoutingError:No route matches [GET] "/items/first/to_cart"
'第一'因爲友好的id。 我做錯了什麼?
您可以將您的routes.rb?您需要在resources:items行添加'member::to_cart'。 –