views:

111

answers:

2

I'm trying to run my site off the lazy idea of not having user registrations.

Anyway, I want a user to be able to "favorite" items on the site when they click "favorite" off of an "item"

I'm assuming I need to use cookies for this but I don't really know the next step. Could anyone point me in the right direction?

Thanks!

+2  A: 

Not rails specific, but I dislike the idea of storing too much stuff in a cookie. Instead I store it on a database on my web site, and just put a primary key value in the cookie. That way you can store as much or as little as you like without transmitting too much over the net. The disadvantage of the "no registrations" approach is that they lose all their stored data if they change to a new computer, or even a new browser on the same computer.

What I do in my app is to store a cookie with a "session id". Then I have several tables, one stores one-off data like the last date that session id was seen, and others store multiple items per session id. I have a "session_states(session_id, state)" table, for instance, that stores one record for each state a person chooses from a list of US states.

One reason for storing the last date is that I purge any session ids from the database that haven't been seen in two years (because I give an expiration of +2 years when I create the cookie).

Paul Tomblin
So let's say you had a table of favoritesFor each item that a user favorites I would create a new favorite record with the id of the item and then create a cookie that points to the favorite primary key value?Any psuedo code possible? :)
DFischer
Also, a cookie can only be a string value right? How would I iterate over a collection of favorited items to display to the user if I can't get an array of favorites?
DFischer
A: 

Maybe you could learn more about cookies on Rails. This link has a lot of information about this topic. I think that you will appreciate it!

But take some care to not store too much information using cookies.

Pedro Ghilardi