I am new to Ruby and to Rails (using Ruby 1.9.3 and Rails 3.2.3) so I
picked
up the Agile Web Development book and have been working through
it
(apologies if this isn't the correct place for this question but the
pragprog forums don't seem to work for me). I am currently working
through the book and have reached the point where I am adding
functionality to an "Add to Cart" button. Here is the code that was
provided for the 'create' method in the line_item_controller:
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
solution:
1.@line_item = @cart.line_items.build
@line_item.product = product
2.
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
attr_accessible :cart_id, :product_id
attr_accessible :product
=begin
If you want to be able to do line_items.build(product: p) you need to
mark
product as attr_accessible too. The mass assignment stuff doesn't
know what
things are database attributes, virtual attributes,
associations etc.
=end
end