Checking cookies

Hi,
may be a fairly redundant question, as i realize this is not much of a
problem as it is lack of knowledge and capability to find some useful
information on the net;

setting a cookie is easy enough, as i did:

cookies[“survey_#{@survey.id}”] = {:value => ‘abc’}

which works fine, as i checked my browser, and it saved it perfectly.
i was wondering how i check if the user has that cookie or not.
say, in the rhtml i want to block some code if the user has this cookie,
namely:

<% if cookie[“survey_#{@survey.id}”] -%>
…you can’t reach this, you already did the survey…
<% else -%>
…blockofcode
<% end -%>

but unfortunately the cookie[…] doesn’t work. how do i check if a user
has a cookie set in his browser or not?
many thanks for an answer or a lead;

thx,

harp

harp wrote:

<% if cookie[“survey_#{@survey.id}”] -%>
…you can’t reach this, you already did the survey…
<% else -%>
…blockofcode
<% end -%>

but unfortunately the cookie[…] doesn’t work.

That should be

cookies[…]

Note the ‘s’.

Chris

Chris M. wrote:

That should be

cookies[…]

Actually, that’s not the main problem. The main problem is that the
‘cookies’ method isn’t accessible from views. You can only access it
from your controller.

So in your controller, write something like:

@completed = cookies[“survey_#{@survey.id}”]

And then in your view you can do

<% if @completed -%>

Chris

many thanks. i didn’t realize the cookies method wasn’t avaliable in the
views…
thx,

harp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs