Ruby newbie here. I'm going through Agile Web Development With Rails. In chapter 11 it challenges you to add a 'decrease quantity' button to items in the shopping cart. I went ahead and tried to implement an increase link as well.
The problem is it's not doing anything when I click on the links.
line_items_controller.rb
def decrease
@cart = current_cart
@line_item = @cart.decrease(params[:id])
respond_to do |format|
if @line_item.save
format.html { redirect_to store_path, notice: 'Item was successfully updated.' }
format.js { @current_item = @line_item }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @line_item.errors, status: :unprocessable_entity}
end
end
end
def increase
@cart = current_cart
@line_item = @cart.increase(params[:id])
respond_to do |format|
if @line_item.save
format.html { redirect_to store_path, notice: 'Item was successfully updated.' }
format.js { @current_item = @line_item }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
cart.rb
def decrease(line_item_id)
current_item = line_items.find(line_item_id)
if current_item.quantity > 1
current_item.quantity -= 1
else
current_item.destroy
end
current_item
end
def increase(line_item_id)
current_item = line_items.find(line_item_id)
current_item.quantity += 1
current_item
end
routes.rb
resources :line_items do
put 'decrease', on: :member
put 'increase', on: :member
end
_line_item.html.erb
<% 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><%= link_to "-", decrease_line_item_path(line_item), method: :put, remote: true %></td>
<td><%= link_to "+", increase_line_item_path(line_item), method: :put, remote: true %></td>
<td><%= button_to 'Remove Item', line_item, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
/line_items/increase.js.erb
$('#cart').html("<%= escape_javascript(render(@cart)) %>");
$('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}, 1000);
/line_items/decrease.js.erb
$('#cart').html("<%= escape_javascript(render(@cart)) %>");
$('#current_item').css({'background-color':'#88ff88'}).animate({'background-color':'#114411'}), 1000);
if ($('#cart tr').length==1) {
// Hide the cart
$('#cart').hide('blind', 1000);
}
Let me know if I forgot anything crucial. Thanks in advance!
----EDIT----
I changed the code to what Rich posted, and this is what shows up in the console when I click the '+' link.
Started GET "/line_items/25/qty" for ::1 at 2016-01-30 23:49:11 -0600
ActionController::RoutingError (No route matches [GET] "/line_items/25/qty"):
So I see that it needs a route for qty but I'm not quite sure how to set that up. I'm guessing the JS alert we set up isn't firing because it's snagging up at this point?
----EDIT 2----
Now I'm passing the links as POST
and getting this name error, from both the up and down links:
Started POST "/line_items/25/qty" for ::1 at 2016-01-31 09:49:04 -0600
Processing by LineItemsController#qty as JS
Parameters: {"id"=>"25"}
Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.0ms)
NameError (undefined local variable or method `current_cart' for #<LineItemsController:0x007fbfb11ea730>):
app/controllers/line_items_controller.rb:70:in `qty'
What confuses me here is that current_cart
works in the increase
and decrease
methods but not in qty
.