Wenn ich versuche, Produkte zu einem Warenkorb in einer App hinzuzufügen, die ich baue, bekomme ich immer diesen Fehler Couldn't find Product with 'id'=
. Laut Better Errors geschieht dies in der ersten Zeile der Create-Methode in meinem product_items_controller.rb
, wie unten zu sehen ist.Konnte Produkt mit 'ID' = nicht finden, wenn Produkte zum Warenkorb in Schienen App hinzufügen
def create
@product = Product.find(params[:product_id])
@product_item = @cart.add_product(product.id)
if @product_item.save
redirect_to root_url, notice:'Product added to Cart'
else
render :new
end
end
änderte ich die erste Zeile: @product = Product.find(params[:id])
aber nicht den Fehler korrigieren haben.
Frühere heute Abend ich geändert, um den Add to Cart
Button-Code aus: <%= button_to product_items_path(product_id: product) do %>
-<%= button_to product_items_path(@product) do %>
Dies ist der Code für die Schaltfläche In der Warenkorb, wie es jetzt ist.
<%= button_to product_items_path(@product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>
UPDATE ADDED routes.rb
hier ist die routes.rb
Rails.application.routes.draw do
resources :categories
resources :labels
resources :products
resources :carts
resources :product_items
resources :orders
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
root 'pages#index'
end
ANOTHER EDIT
hier die cart.rb
ist, die die add_product Methode hält
class Cart < ActiveRecord::Base
has_many :product_items, dependent: :destroy
def add_product(product_id)
current_item = product_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = product_items.build(product_id: product_id)
end
current_item
end
def total_price_usd
product_items.to_a.sum{|item| item.total_price_usd}
end
def total_price_isl
product_items.to_a.sum{|item| item.total_price_isl}
end
end
UPDATE
hier ist der Link zum GitHub Repo https://github.com/DadiHall/hlinreykdal
ich alle Artikel über Active Admin erstellt haben, und das funktioniert, wie es funktionieren sollte. Fehle ich hier etwas? Ich kann nicht herausfinden, warum dieser Fehler immer wieder kommt.
Zu welcher Aktion wird die Route 'product_items_path (@product)' zugeordnet? Kannst du deine routes.rb posten? Und was sind '@ cart' und' product' innerhalb der 'create' Aktion? –
Hallo, ich habe die routes.rb und die cart.rb hinzugefügt .... die add_product Methode ist in der cart.rb – DaudiHell
Was ist 'Produkt' in der zweiten Zeile von' create' Aktion? –