I don’t understand 5th line in “line_items_controller.rb” :
I notice the code for Rails 3.1 don’t have such line while in code for Rails 3.2 it has such line.
From Java world, It’s hard to tell what kind of magic ruby used here 🙁
I was stuck in understanding Rails document.
e.g.
button_to has signiture button_to(name, options = {}, html_options = {})
But in the code, you can add on parameter like
<%= button_to 'Empty cart', @cart, :method => :delete, :confirm => 'Are you sure?' %>
I suppose @cart shouldn’t be there…
def create
@cart = current_cart #this is a function method
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
@line_item.product = product
respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart }
format.json { render json: @line_item,
status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors,
status: :unprocessable_entity }
end
end
end
Full source code for project here:
https://github.com/ilovejs/depot_i/blob/master/app/controllers/line_items_controller.rb
it seems like
@cart.add_product(product.id)if to add a product to the@cart, and@line_item.product = productis also seems like doing the same thingits little hard to tell without seeing the code of
Cartmodel, however I thinkremoving the
@line_item.product = productline should also work without a problem