Cookies and Ruby?

When someone comments on my site, I would like to store the user’s name
as a cookie, and display it the next time they return. I really don’t
know how to do this. Does this use the cgi library? As most things
with ruby, I would imagine it to be quite simple… could anyone post a
code snippet as to how this would work??? Thanks…

ry an wrote:

When someone comments on my site, I would like to store the user’s name
as a cookie, and display it the next time they return. I really don’t
know how to do this. Does this use the cgi library? As most things
with ruby, I would imagine it to be quite simple… could anyone post a
code snippet as to how this would work??? Thanks…

Do you want to store it on the user’s machine or the server?

If it simply storing a name as a cookie, you can use javascript or rails
to write a cookie.

In a controller method that will write the cookie, use:

cookies[:cookie_with_name] = {:value => ‘User Name’, :expires =>
1.year.from_now}

In a controller method that will remove the cookie, use:
cookies.delete :cookie_with_name

That should do it for setting and removing the cookie. Now you need to
figure out how to extract the name from cookies[:cookie_with_name].

Bill

Very small amounts of Googling produced this:

http://blog.nanorails.com/pages/rails_1.1_cheat_sheet

Ok, thanks. Does anyone know how to extract the cookie value?

cookies[:cookie_with_name].value #???

Thanks…

since cookies is a hash:
cookies[:cookie_with_name]

2006/8/15, ry an [email protected]:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Michael S. [email protected]

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium