Webbr88
Webbr88

Reputation: 89

Unique Session ids in Rails

I made a shopping cart model for a webapp that I am working on. It was just a standard ruby on rails model.

It lets users add products to the shoppingcart, but only shows it to them if they are the user that added the item. However, I'd like to let users who haven't signed in add items to a shopping cart.

Right now I'm using devise so I can check for a current_user, and assign that shopping cart item to that user. However is there a similar unique session id or anything I can use to simulate a user. So I only show the items that were added to the one person?

The reason for this is I'd like my user to go through as much of the ordering process as possible before asking them to sign-in and make an account.

Thanks

Upvotes: 2

Views: 1396

Answers (2)

SujitS
SujitS

Reputation: 11423

I worked on similar problem. What I did was, I used session_id. First, you need a table(say, product_list) to hold the product list for temporary purpose. Populate each product to this table along with session_id. This will help you to identify the association of product_list with the active user. On checkout copy the record from product_list to your shopping_cart table and delete the record from product_list.

Try this, it might help you with the current problem.

Upvotes: 0

tadman
tadman

Reputation: 211680

A simple method for this is creating a shopping cart and then adding that identifier to the session regardless of their logged in state.

For example:

@shopping_cart = ShoppingCart.create(user: current_user)
session[:shopping_cart_id] = @shopping_cart.id

Later you can retrieve the current shopping cart, if any, as a before_action handler.

Upvotes: 5

Related Questions