簡體   English   中英

Rails 會話變量(購物車)

[英]Rails session variable (shopping cart)

我正在嘗試將客戶可以添加到我的購物車中的項目保存在會話變量中。 下面是我在購物車控制器中的索引方法。 項目的 ID 保存在@idtemp 中。 接下來我創建一個新的數組 session[:content]。 然后找到@idtemp中保存有ID的item,將其作為數組返回,用於視圖中顯示表格。 問題是,每次添加商品並調用index函數時,session[:contenc]設置為[],刪除購物車,這意味着新添加的商品會覆蓋上一個。 現在我想要修復這個問題,以便將新添加的項目添加到數組中而不覆蓋最后一個,但我不知道如何。 我試圖在索引之外初始化會話變量,但這並沒有以某種方式起作用。 我知道這很容易,但我已經筋疲力盡,無法弄清楚。

定義索引

  @idtemp = session[:cart]

  session[:content] = []
  session[:content] = session[:content] << (Artikel.find([@idtemp]))

  @cart = session[:content]

結尾

- - -看法:

<% @cart.each do |id,quantity| %> ... ...

如果我是你,我會將購物車及其內容存儲在數據庫中。 然后會話要做的就是記住購物車 id 或用戶的 id,如果他們登錄了(一旦你有了用戶,你就可以得到購物車)。 我會設置類似這樣的東西(您可以根據自己的架構進行調整 - Artikle 或其他任何東西而不是 Product)

class Cart
  belongs_to :user
  has_many :cart_products

class CartProduct
  belongs_to :cart
  belongs_to :product
  #extra field - quantity

當頁面加載時,查看是否有 session[:cart_id]。 如果這樣做,您可以加載購物車及其相關產品(如果需要)。 如果沒有,則創建一個購物車對象並將 session[:cart_id] 設置為該對象。

如果您有一個登錄用戶(即 session[:user_id] 或類似的),那么您可以將current_user設置為該用戶,並設置他們的購物車。

我會通過 ApplicationHelper 中的一些受保護的方法來管理它

#in application.rb
def current_user
  if @current_user
    return @current_user
  elsif session[:user_id] && user = User.where(:id => session[:user_id]).first
    @current_user = user
    return @current_user
  end
end

#there's a bit of repetition in here but it should give you the idea
def current_cart
  unless @current_cart
    if current_user
      cart = current_user.cart || Cart.create(:user => current_user)
      @current_cart = cart
    elsif session[:cart_id]
      @current_cart = Cart.where(:id => session[:cart_id]).first || Cart.create
    else
      @current_cart = Cart.create
    end
  end
  return @current_cart
end

現在,每當您想在控制器和視圖代碼中引用當前用戶或當前購物current_user ,只需說current_usercurrent_cart 每個方法中的實例變量意味着如果它在這個請求中設置了一次,那么它不會再次使用邏輯——它只會使用它已經保存在實例變量中的那個。

好的,調整您現有的系統,我會這樣做。 我們將有一個會話變量,它是一個散列,存儲了 Artikel 的 id 和每個的數量,結構如下: session[:cart] = {123 => 1, 456 => 2}其中 123 和 456 是 Artikel身份證。

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

def index 
  session[:cart] ||= {}
end

現在在你看來你可以說

<% session[:cart].each do |artikel_id, quantity| %>
  <% artikel = Artikel.where(:id => artikel_id).first %>
  ...

這是我不明白的:

def index
@idtemp = session[:cart]               # @idtemp is now an ID e.g. '1'
session[:inhalt] = []                  # session[:inhalt] is now an empty array []
addedarticle = Artikel.find(@idtemp)   # addedarticle is an article e.g.:{ID=>'1',title=>'Mouse'}
session[:inhalt].concat([addedarticle]) # session[:inhalt]  is now  [{...}] where {...} = article
@cart = session[:inhalt]                # @cart = [{...}]
end

這有效。 一個項目被添加到購物車中,@cart 是一個包含這個項目的數組,它現在可以在視圖中使用。 現在唯一要做的就是確保下一個添加的項目不會覆蓋舊的 ond。 你說我可以簡單地用 session[:inhalt] ||= [] 替換 session[:inhalt] = [],但是,這樣做會導致錯誤:

購物車中沒有方法錯誤#index

“#”的未定義方法`title':字符串

<% @cart.each do |id| %>
        <tr>
          <td><%= image_tag id.Image_URL %></td>
          <td>
            <div class="title"><%=  id.title %></div>
            <br>

在我看來,@cart 沒有正確“填充”。 它應該是一組文章(=哈希),例如 [{id => 1, title ='mouse'}, {id => 2, title = 'whatever'}],所以我可以用每個迭代它do 方法並顯示它們。 但根據錯誤,它試圖從字符串中提取標題。 可能是什么原因造成的? 如果我能解決這個問題,我就完成了任務。 順便說一句,謝謝你到目前為止幫助我:)

暫無
暫無

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

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