Sure. Here we go:

#config/session_store.rb
Locum::Application.config.session_store :cookie_store, :key => '_locum_session'

#ApplicationController.rb
  def current_order
    if session[:order_id]
      current_order = Order.find(session[:order_id])
    end
    if session[:order_id].nil?
      current_order = Order.new
    end
    current_order
  end
  
  def current_order?
    session[:order_id]
  end

#OrdersController (inherits from Application Controller)
  before_filter :check_for_existing_order, :except => :except => [:add_item]

 # Users cannot see any other action on the app until they have gone through 
add_item.
 def add_item 
    @order = current_order
    @order.save
    session[:order_id] = @order.id
    variant = Variant.where(:sku => params[:variant_sku]).first
    quantity = params[:quantity].to_i
    @order.add_item(variant, quantity) unless variant.nil?
    redirect_to order_path
  end

  def show
    @order = current_order
  end

  def update
    @order = current_order
    @order.attributes = params[:order]
    if @order.save
      flash[:notice] = 'Your cart has been updated successfully'
    else 
      flash[:alert] = "Your cart could not be updated: 
#{@order.errors.full_messages.join(',')}"
    end
    redirect_to order_path 
  end

  protected
  # This is where I am caching the exceptions.
  def check_for_existing_order
    unless current_order?
      begin 
        raise "Expecting to have order, but session doesn't have one"
      rescue => exception
        ExceptionNotifier::Notifier.exception_notification(request.env, 
exception).deliver
      ensure 
        redirect_to root_path unless Rails.env.development?
        return
      end
    end
  end

#config/routes.rb
  resource :order, :except => [:destroy] do
      member do
        get :checkout
        post :add_item
      end
    end

I added the important parts (I think). Thanks for the help!

-- 
Ylan Segal
[email protected]

On Dec 29, 2011, at 9:24 AM, Ben Wanicur wrote:

> Can you post your code Ylan ?  Also, can you include and session store config 
> files as well ?
> 
> On Thu, Dec 29, 2011 at 9:21 AM, Ylan Segal <[email protected]> wrote:
> Fellow Rubyists,
> 
> I have been scratching my head on this one for a while: I have an e-commerce 
> app in production with pretty standard add-to-cart functionality. Whenever a 
> user starts an order, I add the order id to the session and read it back on 
> subsequent requests (like showing the cart, checkout form, etc). By and 
> large, this works as expected in my tests and testing with local browsers.
> 
> However, for a small number of my users, the session doesn't seems to keep 
> the order id. In actions where I would expect the order id to be there, it is 
> not. For example, I have a OrdersController#update action that, well, updates 
> the order. Following convention, that actions forwards to 
> OrdersController#show. Since the session[:order_id] was present in #update, I 
> would obviously expect it to be set in #show, and for most users it is, but 
> for some it is not. I am obviously, checking that the user is not visiting 
> #show before #update or #create has been called.
> 
> Random stuff that I think might make a difference
> 
> * It is not restricted to any one browser/version. It happens with IE, 
> Firefox, etc.
> * It happens with browsers that have cookies (I can see when analyzing 
> requests, that the cookies headers are being sent, it just that the session 
> doesn't contain the expected variable/value).
> * Running Rails 3.1 on heroku with default session store.
> 
> Currently, my only hunch is that the users browser is not sending the latest 
> version of a cookie (with the order_id set in the session) but the original 
> one generated when the first visited the site (where the session[:order_id] 
> has not been set).
> 
> Has anyone experiences something like this? Would using another kind of 
> cookie store solve this?
> 
> Thanks for the help,
> 
> --
> Ylan Segal
> [email protected]
> Tel: +1-858-224-7421
> Fax: +1-858-876-1799
> 
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> 
> 
> -- 
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to