簡體   English   中英

添加購物車項目Ruby on Rails

[英]Adding Cart items Ruby on Rails

我正在開發一個非常基本的購物車,我遇到的問題是,如果我在購物車中添加了多個相同的產品,我沒有看到數量增加,而是只看到該項目的多個版本。

例如,我看到:

1 x綠燈= 15英鎊1 x綠燈= 15英鎊

而不是看到:

2 x綠燈= 30英鎊

我怎樣才能使購物車檢查購物車中的多個版本然后將它們一起添加?

目前我有:

應用控制器

  def current_cart
  if session[:cart_id]
    @current_cart ||= Cart.find(session[:cart_id])
  end
  if session[:cart_id].nil?
    @current_cart = Cart.create!
    session[:cart_id] = @current_cart.id
  end
  @current_cart
  end 

推車控制器

   def show
    @cart = current_cart
   end

購物車模型

 has_many :items

 def total_price
   items.to_a.sum(&:full_price)
 end

購物車視圖

 <table id="line_items">
   <tr>
     <th>Product</th>
     <th>Qty</th>
     <th class="price">Unit Price</th>
     <th class="price">Full Price</th>
   </tr>

  <% for item in @cart.items %>
    <tr class="<%= cycle :odd, :even %>">
       <td><%=h item.product.name %></td>
       <td class="qty"><%= item.quantity %></td>
       <td class="price"><%= gbp(item.unit_price) %></td>
       <td class="price"><%= gbp(item.full_price) %></td>
    </tr>
   <% end %>
 <tr>

  <td class="total price" colspan="4">
    Total: <%= gbp(@cart.total_price) %>
  </td>
  </tr>
  </table>

更多信息

產品#指數

  <%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post %>

人們可以提供的任何建議將非常感激。 謝謝!

新設置 - 導致錯誤Uninitialised Constant CartController

的routes.rb

 Checkout::Application.routes.draw do

   ActiveAdmin.routes(self)

   devise_for :admin_users, ActiveAdmin::Devise.config

   post '/add_to_cart/:product_id' => 'cart#add_to_cart', :as => 'add_to_cart'

   resources :carts
   resources :products
   resources :items

   root :to => 'products#index'

 end

推車控制器

 class CartsController < ApplicationController

   def show
     @cart = current_cart
   end

   def add_to_cart
       current_cart.add_item(params[:product_id])
       redirect_to carts_path(current_cart.id)
   end

 end

推車模型

class Cart < ActiveRecord::Base
has_many :items

def add_item(product_id)
        item = items.where('product_id = ?', product_id).first
    if item
        # increase the quantity of product in cart
        item.quantity + 1
        save
    else
        # product does not exist in cart
        product = Product.find(product_id)
        items << product
    end
    save
end

def total_price
    items.to_a.sum(&:full_price)
end
end

產品編號指數

<table class="jobs">
    <thead>
        <tr>
            <th scope="col" id="name">Product Code</th>
            <th scope="col" id="company">Name</th>
            <th scope="col" id="company">Price</th>
            <th scope="col" id="company">Action</th>
        </tr>
    </thead>

    <tbody>
        <% @product.each do |product| %>
        <tr>    
            <td><%= product.product_code %></td>
            <td><%= product.name %></td>
            <td><%= gbp(product.price) %></td>
            <td><%= button_to "Add to Cart", add_to_cart_path(:product_id => product), :method => :post %></td>
        </tr>
        <% end %>
    </tbody>
</table>

在Cart模型中,創建一個名為的方法

def add_item(product_id)
  item = items.where('product_id = ?', product_id).first
  if item
    # increase the quantity of product in cart
    item.quantity + 1
    save
  else
    # product does not exist in cart
    cart.items << Item.new(product_id: product_id, quantity: 1)
  end
  save
end

在routes.rb中,

post '/add_to_cart/:product_id' => 'cart#add_to_cart', :as => 'add_to_cart'

將“添加到購物車”路徑更改為Cart控制器中的調用add_to_cart方法。

def add_to_cart
  current_cart.add_item(params[:product_id])
  # redirect to shopping cart or whereever
end

這應該讓您了解您想要完成的任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM