small medium large xlarge

Generic-user-small
21 May 2010, 14:24
neocoolstar (5 posts)

I finished Iteration I2, but partway through I hit the “Add to Cart” button and nothing happens. Looking at the log, the error that appears is:

Processing StoreController#add_to_cart [POST] {[info edited out]}

NoMethodError (undefined method add_product' for nil:NilClass): app/controllers/store_controller.rb:11:in add_to_cart’

:8:in `synchronize' C:/Ruby19/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' C:/Ruby19/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' C:/Ruby19/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' This is what the add_to_cart method in store_controller.rb looks like: ~~~ def add_to_cart product = Product.find(params[:id]) @current_item = @cart.add_product(product) respond_to do |format| format.js if request.xhr? format.html {redirect_to_index} end rescue ActiveRecord::RecordNotFound logger.error("Attempt to acess invalid product #{params[:id]}") redirect_to_index("Invalid product") end ~~~ The add_product in cart.rb: ~~~ def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity else current_item = CartItem.new(product) @items << current_item end current_item end ~~~ The index.html.erb within depot/views/store ~~~

<%= I18n.t 'main.title' %>

<% @products.each do |product| -%>
<%= link_to image_tag(product.image_url), {:action => :add_to_cart, :id => product}, :method => :post %>

<%=h product.title %>

<%= product.description %>
<%= number_to_currency(product.price) %> <% form_remote_tag :url => { :action => 'add_to_cart', :id => product } do%> <%= submit_tag I18n.t('main.button.add') %> <% end %>
<% end %> ~~~ Help please, thanks!
Dave_gnome_head_isolated_pragsmall
21 May 2010, 22:37
Dave Thomas (390 posts)

What sets @cart in the controller?

Generic-user-small
25 May 2010, 21:20
neocoolstar (5 posts)

What do you mean?

You must be logged in to comment