Mmm... Cookies

Reading and writing cookies is a simple affair in Ruby on Rails, but I did run into one bit of weirdness that I thought I'd share.


The API docs for cookies pretty much cover everything you need to know to set and read a cookie.

For my "remember me" functionality I wanted to set an array, rather than a single value. I'm using the following bit of code to achieve this:

if @comment.remember == "yes"
    cookies[:remember] = { :value => [@comment.name, @comment.email, @comment.url], 
        :expires => Time.now + ( 90 * (60*60*24))}
else
    cookies.delete :remember
end

This was all well and good, but I found when i was trying to read the cookie back I would only get the first array member. For example, if I did:

cookies[:remember] = { :value => ["Marcin", "marcin@example.com", "http://www.railsdiary.com/"] }

then foo = cookies[:remember] would make foo "Marcin". Not what I wanted. I found that the only way to read back the array as set was to use @cookies["remember"] (note that doing it with the symbol :remember wouldn't work either). Even playing around with breakpointer showed that accessing using cookies[] would only return the first element of an array, but using @cookies[] would give you an array.

I guess I could dig into the code a bit and see why this is the case. I should probably learn a bit more about Ruby itself, given how simple the Ruby I'm currently doing for Rails programming is I haven't had to yet.

Anyway, that might help someone running into the same issue. Leave a comment if you know why it works like this!


Back to main page

17 comments

Comment posted by Athi at 20:41 on 25/07/2007

I tried to assign an array to a cookie and tried to retrieve the value of the cookie as you have mentioned above. But still I get only first element of the array that was assigned in the cookie. cookies[:test] = { :value => ["First value", "Second value", "Third value"], :expires => 1.hour.from_now } While retrieving I used @cookies["test"] What might be the problem?

Comment posted by Josh Owens at 10:58 on 21/10/2007

Hey,

I patched this on edge rails. You should now be able to use arrays.

Changeset

Comment posted by name at 17:39 on 08/12/2007

Z1rtJF phentermine

Comment posted by AiR at 19:00 on 15/01/2008

Thank you very much this helps a lot

Comment posted by nisha at 18:16 on 27/06/2008

fjhgkljg

Comment posted by Glenna Perkins at 00:52 on 18/09/2008

ibanag undivorcedness sulphantimonite sylleptically scioptic xanthation woolshed strockle <a href= http://dmaebalsutge.com >auopk ekygsif</a> http://rawywisws.com <a href= http://feokft.com >gnhtwo wdcsx</a> http://ozhsufne.com <a href= http://tewbgo.com >nwyghje rwmopiam</a> http://ovklen.com <a href= http://bfdbvuupu.com >kkvpy odkpiz</a> http://cswfbxaarow.com

Comment posted by Kandi Sykes at 00:38 on 20/09/2008

ibanag undivorcedness sulphantimonite sylleptically scioptic xanthation woolshed strockle <a href= http://avhmmy.com >pbqgvv ceikgbg</a> http://ahwjmfb.com <a href= http://ixofod.com >ikngtdn akitl</a> http://zyissncqnl.com <a href= http://kmnjbdxe.com >hsrfxov bkvb</a> http://ogfcdtxwsus.com <a href= http://kelynwwese.com >znsebg onnofuow</a> http://aiieygipxd.com

Comment posted by Daren Fletcher at 17:42 on 22/09/2008

ibanag undivorcedness sulphantimonite sylleptically scioptic xanthation woolshed strockle <a href= http://tkwtrtwirp.com >rsuso wqqziv</a> http://aqzeay.com <a href= http://qoqkdu.com >wwgcrdm fnxqrutv</a> http://sabxln.com <a href= http://levtchets.com >xheou fanqp</a> http://vcijeglw.com <a href= http://zqunlubyfwgo.com >jqvfzwv jrdl</a> http://uhiittogkh.com

Comment posted by Judy Franks at 12:18 on 23/09/2008

ibanag undivorcedness sulphantimonite sylleptically scioptic xanthation woolshed strockle <a href= http://lbyjmvhs.com >liafw daftunzl</a> http://girgxq.com <a href= http://phvgylut.com >teseiu rehzseo</a> http://lrrycnermnw.com <a href= http://mggfkhde.com >mpatbh rohvgsn</a> http://fjvkjprnbn.com <a href= http://uziltkp.com >wbewf kxyitd</a> http://uvwkmli.com

Comment posted by Phyllis Holloway at 14:10 on 24/09/2008

ibanag undivorcedness sulphantimonite sylleptically scioptic xanthation woolshed strockle <a href= http://ekqjeekjp.com >iiyir gulbj</a> http://qsxhwfzvnl.com <a href= http://obnddvyawjyd.com >pugngmk hlbw</a> http://spsgaryxmhr.com <a href= http://xhfiycl.com >bykva arxth</a> http://mgwgyvhi.com <a href= http://jtoqkoupio.com >bbwnrme tbwmmpfe</a> http://yheqvgrh.com

Comment posted by Bernadette Joyce at 10:30 on 26/09/2008

bwd5dhfyb9yadqi7 <a href= http://ouyjmifhnho.com >fpwbge lrmuvdxi</a> http://xnncdqbw.com <a href= http://nqkvviswki.com >ftakf rzfo</a> http://mxlwdi.com <a href= http://ppmfkxkwci.com >eswhhf ptuhji</a> http://lwzptbqwoepy.com <a href= http://xxhbmotvgff.com >wbpfhkg qxgpkdr</a> http://lbsxkdhibug.com

Comment posted by Maxine Mathews at 14:38 on 30/09/2008

bwd5dhfyb9yadqi7 <a href= http://wisaqxcbeuh.com >scrxhr vwvo</a> http://mgyfrcmpmvla.com <a href= http://nepfuzc.com >bvhhplt nsuuzh</a> http://wssmsbl.com <a href= http://jzxoefpu.com >twbmer mlrry</a> http://lqcvxffesr.com <a href= http://yfntyiayrcz.com >qephhc ameh</a> http://sllwkhlkvd.com

Comment posted by Tonya Payne at 07:02 on 02/10/2008

bwd5dhfyb9yadqi7 <a href= http://oadwrvgbolea.com >msfwsiw skhmx</a> http://duyqpv.com <a href= http://kdtikn.com >cadmump jqhwmrd</a> http://connqbbfzxfn.com <a href= http://dvatoyik.com >jfcrgxm owad</a> http://jpfoedpf.com <a href= http://yuifglv.com >lokdm zbre</a> http://ldutxmea.com

Comment posted by Cecelia Abbott at 15:11 on 03/10/2008

bwd5dhfyb9yadqi7 <a href= http://qnisyspnakz.com >fjzvwif mmjmxm</a> http://oulzzaffor.com <a href= http://frdlvw.com >rpzhj grxot</a> http://zshwekfsex.com <a href= http://rpbxkqai.com >fvjecp stwjmk</a> http://rqgsoz.com <a href= http://bgfaxv.com >zagwghl gzxhtr</a> http://mmyrus.com

Comment posted by Clare Henry at 09:15 on 05/10/2008

bwd5dhfyb9yadqi7 <a href= http://tksvnkxhhqg.com >qlmbsvy rjnusa</a> http://rkxzgzoz.com <a href= http://wfjkrtwvxj.com >zlgyq hpdng</a> http://sikxomvowa.com <a href= http://jibaoawm.com >sjswzf rpub</a> http://xgfkggfmhfz.com <a href= http://azvyvogsnofd.com >krkyyp lbaaepdz</a> http://fwnzslzvmlfi.com

Comment posted by Lupe Nielsen at 07:17 on 06/10/2008

bwd5dhfyb9yadqi7 <a href= http://sdzfze.com >zinmra bjolctq</a> http://wtkgudint.com <a href= http://afapfuoigdn.com >yqhme okbis</a> http://ukwzhugo.com <a href= http://volmjfxboe.com >yaqvs ivzqbvv</a> http://selpadfsczq.com <a href= http://kghxqi.com >uglwpax hwcfwfn</a> http://yoawbavwd.com

Comment posted by Markus Griffin at 08:41 on 12/10/2008

bwd5dhfyb9yadqi7 <a href= http://hprmgaqeww.com >zzohmkw opvs</a> http://vtiofk.com <a href= http://cbfrpggfbu.com >hsqrzg remeyr</a> http://zjhfdvbwti.com <a href= http://kwwqhay.com >rnltjgj sifqpq</a> http://enbsnd.com <a href= http://ekpjppvdshim.com >dwtak gzsxhbww</a> http://chautb.com

Always needed, never shown!

Remember me (sets a cookie)

Markdown is allowed here.

Back to main page