dimanche 25 juin 2017

Clear cart ruby on rails

I am a beginner Ruby on rails, I am trying to create a small system of cart. I already rather advance well thanks to stackoverflox and some little tutorial on the web. The problem is that I can not empty my cart without error, I would have to create an action that removes the cookie from my application but I can not. Here is the source of my app that works but without the possibility to empty are basket

view/cart/index.html.erb

>         <h1>Votre panier</h1>
>     <!DOCTYPE html>
>     <html>
>     <head>
>       <title></title>
>     </head>
>     <body>
>     
>     <% total = 0 %>
>     
>     <table>
>       <% @cart.each do |id, quantity| %>
>       <% item = Item.find(id) %>
>       <tr>
>           <td class="images"><%= link_to image_tag(item.image_url, :size => "50x50"), item %></td>
>           <td width="160"><%= item.produit %></td>
>           <td width="160"><%= quantity %></td>
>           <td width="160"><%= number_to_currency(item.prix, unit: "€") %></td>
>           </tr>
>           <% total += quantity * item.prix %>
>     
>           <% end %>
>     
>           <tr>
>               <td colspan="4">Total :</td>
>               <td><%= number_to_currency(total, unit: "€") %></td>
>           </tr>
>       
>     </table>
>
>       # Please, i want empty my cart !
>       <%= link_to 'Back', items_path %>
>     
>     </body>
>     </html>

cart_controller.rb

class CartController < ApplicationController
  def index
    @cart = session[:cart] || {}
  end

  def add
    id = params[:id]
    cart = session[:cart] ||= {}
    cart[id] = (cart[id] || 0) + 1

    redirect_to :action => :index

  end


end

routes.rb

Rails.application.routes.draw do

resources :items      

get 'cart/index'

match ':controller/:action/:id', via: [:get, :post]

root :to => "items#index"

end

Aucun commentaire:

Enregistrer un commentaire