简体   繁体   English

Ruby 在轨道上:如何在 ajax 购物车中更改购物车中的数量项目,购物车保存在 session,而不是数据库?

[英]Ruby on rails: How to ajax cart when change quantity items in cart, cart save in session, not database?

UPDATE: i found solution for my problems.更新:我找到了解决问题的方法。 Thanks for your support!谢谢你的支持!

I'm doing simple shopping cart with ajax by ruby on rails.我正在用 ajax by ruby 在轨道上做简单的购物车。

I want to ajax my cart page when change quantity item of cart.更改购物车的数量项目时,我想 ajax 我的购物车页面。 I found some solutions but they save cart into database.我找到了一些解决方案,但他们将购物车保存到数据库中。 How to resolve my solution without save cart into database?如何在不将购物车保存到数据库的情况下解决我的解决方案?

I wonder:我想知道:

  • Can i use ajax in file view or include ajax file in view?我可以在文件视图中使用 ajax 或在视图中包含 ajax 文件吗? As way i tried in PHP?正如我在 PHP 中尝试过的那样?
  • If i use ajax default on rails, how to render input field(change value) in file increment/decrement_item.js.erb (file render partial without reload page)?如果我在 Rails 上使用 ajax 默认值,如何在文件 increment/decrement_item.js.erb 中呈现输入字段(更改值)(文件呈现部分而不重新加载页面)? I don't use cart as active record, so render @cart or @item is impossible(or i dont know how to make it possible).我不使用购物车作为活动记录,所以渲染 @cart 或 @item 是不可能的(或者我不知道如何使它成为可能)。

I also have some idea for my problem.我对我的问题也有一些想法。 But it's so hard.但这太难了。 Exp: I think i will create a _quantity_item display input quantity. Exp:我想我会创建一个 _quantity_item 显示输入数量。 And i can use it for index view of cart and render in increment/decrement.js.erb but fail.我可以将它用于购物车的索引视图并以增量/减量.js.erb 呈现但失败。 So sad!好难过!

My view display cart(short):我的视图展示车(短):

<% @cart.each_with_index do |item, index| %>
      <tr>
        <th scope="row"><%= index %></th>
        <td><img src="<%= asset_path(item[0].image) %>" style="width:100px"></td>
        <td><%= item[0].name %></td>
        <td><%= money_vn_format(item[0].price) %></td>
        <td>
          <div class="quantity">
            <%= button_to '-', decrement_item_path(item[0].id), class: 'btn minus1', method: :put, remote: true %>
           <input class="quantity" min="0" value="<%= item[1] %>" type="number"> 
             <%= button_to '+', increment_item_path(item[0].id), class: 'btn add1', method: :put, remote: true %>
          </div>
        </td>
        <td><%= money_vn_format(item[0].price * item[1]) %></td>
        <td><%= link_to 'Remove', delete_cart_path(item[0].id) %></td>
      </tr>
    <% end %>

and code CartsController:和代码 CartsController:

# frozen_string_literal: true

class CartsController < ApplicationController
  def index
    save_session_to_cart if session.key? :cart
  end

  def add
    session[:cart] ||= {}
    session[:cart][params[:id]] = 1 unless session[:cart].key? params[:id]
    save_session_to_cart

    redirect_to carts_index_path
  end

  def delete
    session[:cart].delete params[:id]
    redirect_to carts_index_path
  end

  def destroy
    reset_session
    redirect_to carts_index_path
  end

  def increment_item
    session[:cart][params[:id]] += 1
    save_session_to_cart
    redirect_to carts_index_path
  end

  def decrement_item
    session[:cart][params[:id]] -= 1 if session[:cart][params[:id]] > 1
    save_session_to_cart
    redirect_to carts_index_path
  end

  private

  def save_session_to_cart
    @cart = []
    @total = 0
    session[:cart].each do |product_id, quantity|
      @cart << [Product.find(product_id), quantity]
      @total += Product.find(product_id).price * quantity
    end
    @cart
  end
end

Check out this answer by crispychicken Rails: How to store data in session?通过crispychicken Rails 查看这个答案:如何在 session 中存储数据? . . The poster suggests playing around with the session variable in rails.海报建议在 rails 中使用session变量。

Or conversely if you want it to be simpler, make use of the browser's local storage using JS.或者相反,如果您希望它更简单,请使用 JS 使用浏览器的本地存储。

I'd do that using the browser's local storage, with javascript.我会使用浏览器的本地存储和 javascript 来做到这一点。 You can save values with this:您可以使用以下方法保存值:

localStorage.setItem('items-count', 1);

And read them back like this:然后像这样读回来:

localStorage.getItem('items-count');

But bare in mind that if the user opens a different browser or uses another device, the shopping cart will be empty.但请记住,如果用户打开不同的浏览器或使用其他设备,购物车将是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM