# Cookies and Ruby?

**URL:** <https://www.ruby-forum.com/t/cookies-and-ruby/64612>\
**Category:** Rails\
**Created:** [August 15, 2006, 3:58am UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612 "2006-08-15T03:58:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [August 15, 2006, 3:58am UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612/1 "2006-08-15T03:58:26Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [August 15, 2006, 4:27am UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612/2 "2006-08-15T04:27:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612/3 "2006-08-15T14:20:46Z")

</div>

Very small amounts of Googling produced this:

[http://blog.nanorails.com/pages/rails\_1.1\_cheat\_sheet](http://blog.nanorails.com/pages/rails_1.1_cheat_sheet)

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [August 15, 2006, 5:13am UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612/4 "2006-08-15T05:13:35Z")

</div>

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

cookies[:cookie\_with\_name].value #???

Thanks…

---

<div class="post-metadata">

**Author:** ![hreyaatnh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hreyaatnh](https://www.ruby-forum.com/u/hreyaatnh)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/cookies-and-ruby/64612/5 "2006-08-15T14:20:57Z")

</div>

since cookies is a hash:  
cookies[:cookie\_with\_name]

2006/8/15, ry an [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium
