Reputation: 784
I have a simple shopping cart application that displays the session's shopping cart with line_items in it on the sidebar of the application layout. I am attempting to add a button at the end of each line item (rendered in _line_item.html.erb) that decrements the quantity in the cart by one using remote: true and some JS. However, when I load the page (before I click anything) I am receiving this routing error from Rails 3.1 in development with no special gems or configurations installed:
Routing Error
No route matches {:action=>"decrement", :controller=>"line_items"}
Here is my method in controllers/line_items_controller.rb, which has been working well for me until now:
def decrement
@line_item = LineItem.find(params[:id])
@line_item.quantity -= 1
if @line_item.quantity.empty?
@line_item.destroy
end
respond_to do |format|
format.js {@current_item = @line_item}
end
end
Here is the entry in routes.rb:
resources :line_items do
member do
post 'decrement'
end
end
Here is the code in the partial _line_item.html.erb, which works fine without the button_to link:
<% if line_item == @current_item %>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
<td><%= button_to 'Remove one', line_item, decrement_line_item_path, remote: true %></td>
</tr>
I must be missing something but I cannot find it. It seems everything is in order to me. What am I doing wrong? Thanks very much.
Edit: here is the error I receive when I use decrement_line_item_path(line_item):
ArgumentError in Store#index
Showing /Users/Michael/Sites/rails/depot/app/views/line_items/_line_item.html.erb where line #9 raised:
wrong number of arguments (4 for 3)
Extracted source (around line #9):
6: <td><%= line_item.quantity %>×</td>
7: <td><%= line_item.product.title %></td>
8: <td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
9: <td><%= button_to 'Remove one', line_item, decrement_line_item_path(line_item), remote: true %></td>
10: </tr>
Upvotes: 2
Views: 456
Reputation: 35308
decrement_line_item_path
expects an argument for the individual item and you're not giving it one.
Try:
<%= button_to 'Remove one', decrement_line_item_path(line_item), remote: true %>
Upvotes: 2