Here is my solution I was kinda hoping to get it to redirect to the cart but I didn’t get it working as I wanted and ended up redirecting back to the catalog and leaving a notice

Show.html.erb


<div class="cart_title">Your Cart</div>
<table>
  <% for item in @cart.line_items %>
    <tr>
      <td><%= item.quantity %>&times;</td>
      <td><%= item.product.title %></td>
      <td class="item_price"><%= number_to_currency(item.total_price) %></td>
      <td><%= button_to 'Delete Line Item', item, :method => :delete, :confirm => 'Are You Sure' %></td>

    </tr>
  <% end %>

  <tr class="total_line">
    <td colspan="2">Total</td>
    <td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
  </tr>

</table>

<%= button_to 'Empty cart', @cart, :method => :delete,
    :confirm => 'Are you sure?' %>

line_items_controller.rb


  def destroy
    @line_item = LineItem.find(params[:id])
    @line_item.destroy

    respond_to do |format|
      format.html { redirect_to(store_url, :notice => 'Line Item Removed' )}
      format.xml  { head :ok }
    end
  end

Redirecting to Cart

In order to get it to redirect to cart, I used the following code. But with this code, I couldn’t get it to post a notice. Any ideas?

line_items_controller.rb


  def destroy
    @line_item = LineItem.find(params[:id])
    @line_item.destroy

    respond_to do |format|
      format.html { redirect_to(@line_item.cart, :notice => 'Item has been removed from your cart.') }
      format.xml  { head :ok }
    end
  end

.h4 Diogo Says

I went a bit further and added a condition to redirect to the store if the cart is empty.


def destroy
    @line_item = LineItem.find(params[:id])
    @line_item.destroy

    respond_to do |format|
      if current_cart.line_items.empty?
        format.html { redirect_to(store_url, :notice=> 'Your cart is empty') }
      else 
        format.html { redirect_to(current_cart, :notice=> 'Item Removed') } 
      end

      format.xml  { head :ok }
    end
  end