0

ユーザーがショッピングカートに何かを追加した後、レールを再度開いた後にブラウザを閉じて(閉じる)セッションを回復し、ユーザーがさらに買い物をできるようにするにはどうすればよいですか...今私はそのようなものを持っています

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :current_cart 
  private
    def current_cart
      Cart.find(session[:cart_id])
      @cart = Cart.find(session[:cart_id])
      rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
    end


end

そして注文後に破壊する:

def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil
    respond_to do |format|
      format.html { redirect_to session[:prev_url],
        :notice => I18n.t(:empty_card) }
      format.json { head :ok }
    end
  end

しかし、RoR にこのセッションを維持するように指示するにはどうすればよいでしょうか?

4

1 に答える 1

2

セッションの代わりに Cookie に保存cart_idするだけで、目的を達成できます。カート情報を取り出す必要がある場合は、Cookie から ID を使用します。

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :current_cart 
  private
    def current_cart
      Cart.find(cookies[:cart_id])
      @cart = Cart.find(cookies[:cart_id])
      rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      cookies[:cart_id] = cart.id
      cart
    end


end

それが役に立てば幸い。

于 2013-01-10T20:29:42.410 に答える