Je travaille sur le tutoriel de dépôt dans Agile RoR. J'ai regardé ceci pendant un moment et ne vois pas une erreur. Qu'est-ce que je rate? Je reçois l'erreur suivante lorsque j'ajoute un article à un panierméthode undefined 'where' erreur RoR
J'ai exécuté la migration.
.. /Users/computername/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:376:in
method_missing' /Users/computername/Documents/rails_projects/depot/app/models/cart.rb:5:in
add_product » /Users/computername/Documents/rails_projects/dépôt/app/controllers/line_items_controller.rb: 46: dans `créer »
Voici ma méthode create
def create
@cart = find_or_create_cart
product = Product.find(params[:product_id])
#@line_item = @cart.line_items.build(:product => product)
@line_item = @cart.add_product(product.id)
..
mon modèle de panier
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
def add_product(product_id)
current_item = line_items.where(:product_id => product_id).first
if current_item
current_item.quantity += 1
else
current_item = LineItem.new(:product_id=>product_id)
line_items << current_item
end
current_item
end
end
Non Bien sûr pourquoi vous voudriez 'Product.find' ... pourquoi ne pas juste' @ cart.add_product (params [: product_id]) ' –