2013-06-04 35 views
0

我是Stackoverflow的新品牌,也是一般網絡開發的新手段。 我一直在學習Rails幾個月,並開始構建一個簡單的應用程序來教我自己。購物車的功能,在Rails 3上的current_cart方法定義

我會盡量簡明扼要,應用程序如下所示:用戶註冊或登錄,以便他可以對自己的產品和客戶執行CRUD操作。他還可以通過電話號碼搜索客戶,檢索找到的客戶或表單以創建新客戶。很簡單。現在,每個客戶都可以放置很多訂單(或購物車,無論您想稱之爲什麼),其中存儲了由給定客戶在給定時間購買的許多產品。 你可以在這裏看到一個可行的例子,http://order-checker.herokuapp.com嘲笑了購物車的功能,看看它應該如何都適合在一起。 用戶:guest,密碼:abcd1234

現在我已經編寫了第二個示例,其中介紹了迄今爲止在購物車功能,訂單項模型,控制器等方面的內容,並且爲簡單起見而沒有用戶身份驗證。 https://github.com/gatosaurio/mockup 。這裏是我的模型,第二個例子:

class Customer < ActiveRecord::Base 
    attr_accessible :name, :street 
    has_many :carts 
end 

class Product < ActiveRecord::Base 
    attr_accessible :name, :price 
end 

class Cart < ActiveRecord::Base 
    belongs_to :customer 
    has_many :line_items 
    attr_accessible :purchased_at 

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

class LineItem < ActiveRecord::Base 
    attr_accessible :cart_id, :product_id, :quantity, :unit_price 
    belongs_to :cart 
    belongs_to :product 

    def full_price 
    unit_price * quantity 
    end 
end 

所以,在客戶中index動作我有一個new_cart_path鏈接,它們中的每一個。在購物車中#new我想要一個所有產品的列表,以及「添加到訂單」的帖子鏈接。

<% @products.each do |product| %> 
    <tr> 
    <td><%= link_to product.name, product_path(product) %></td> 
    <td><%= form_tag(line_items_path, :method => "post") do %> 
     <%= hidden_field_tag(:product_id, product.id) %> 
     <%= submit_tag("Add to Cart") %></td> 
    <% end %> 


    </tr> 
<% end %> 

此新購物車操作的側欄還應顯示與此購物車相關的所有訂單項,可能是這樣的。

<% @cart.line_items.each do |line_item| %> 
    <tr> 
    <td><%= line_item.product.name %></td> 
    <td class="qty"><%= line_item.quantity %></td> 
    <td class="price"><%= number_to_currency(line_item.unit_price) %></td> 
    <td class="price"><%= number_to_currency(line_item.full_price) %></td> 
    <td><%= link_to "Delete", line_item, method: :delete, :limit => 1, data: { confirm: 'Are you sure?'}%></td> 
    </tr> 
<% end %> 
<tr> 
    <td class="total price" colspan="4"> 
    <Total: <%= number_to_currency @cart.total_price %></td> 
</tr> 

現在,here's line_items#創建

def create 
    @product = Product.find(params[:product_id]) 
    if LineItem.exists?(:cart_id => current_cart.id, :product_id => @product.id) 
    item = LineItem.find(:first, :conditions => [ "cart_id = #{current_cart.id} AND product_id = #{@product.id}" ]) 
     LineItem.update(item.id, :quantity => item.quantity + 1) 
    else 
     @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price) 
     flash[:notice] = "Added #{@product.name} to cart." 
    end 
redirect_to new_cart_path 
end 

所以你看,我的具體問題畢竟這是:我會怎麼定義application_controller.rb的current_cart方法?我問這是因爲購物車功能上的所有資源都可以從谷歌搜索中找到(Agile Web Development with Rails,第4版)幾乎完全處理爲購物車實例創建會話對象,但我發現這種方法不適合我的情況,因爲我需要每個購物車只與一個特定客戶相關聯,而不是在應用用戶的級別上進行會話。

我真的希望這是一個有效的問題,如果這太長了,我目前的代表級別不允許我發佈兩個以上的鏈接,但你可以在我的Github帳戶上找到所有代碼。

感謝很多:)

回答

1
# Shopping cart, id is in session (if any) 
    def current_cart 
    Cart.find(session[:cart_id]) 
    rescue ActiveRecord::RecordNotFound 
    cart = Cart.create 
    session[:cart_id] = cart.id 
    cart 
    end 

的車ID在會議期間舉行。如果沒有參考或找到購物車,則會創建一個新購物車並將該ID存儲在會話中。無論哪種方式,都會返回購物車對象。

你最終會有很多空的或陳舊的購物車。記得要設立一個清理工作。

附錄

你的質量分配錯誤是此行的結果:

 @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price) 

在這裏,你的質量分配創建一個新的LineItem。在你的模型,你允許

attr_accessible :cart_id, :product_id, :quantity, :unit_price 

質量分配,這裏是棘手的部分:雖然Rails的內部分配ID,它需要你的配置字面上。要批量分配到cart,您需要attr_accessible :cart。因此,您可以

 @line_item = LineItem.create!(:cart_id => current_cart.id, :product_id => @product.id, :quantity => 1, :unit_price => @product.price) 

attr_accessible :cart, :cart_id, :product, :product_id, :quantity, :unit_price 

既會工作。

+0

感謝您的回答:) 我這樣做了幾天前,我想我應該建立一個過濾器,以清空購物車會議剛剛創建一個新的購物車對象。 問題是我收到以下錯誤: 無法批量分配受保護的屬性:購物車,產品 我的模型顯示在我的原始問題中。我在這裏做錯了什麼? – Gatosaurix

+0

新增瞭解答達人貼回覆 –

+0

謝謝!它現在有效! :) 我可以看到我列出的行項目,因爲我想要的。我重寫了一點,像這樣: 高清current_cart 會話[:cart_id]!|| = Cart.create .ID @current_cart || = Cart.find(會話[:cart_id]) end現在 我我正在努力將每個購物車與特定的客戶相關聯,我想我會嘗試使用嵌套屬性。 現在我想我還應該在CartsController中構建一個after_filter,以便在創建購物車實例後將每個購物車會話設置爲零。 我會讓你知道它是如何去的。 Dankeschön,hab'doch viel gelernt :) – Gatosaurix