Cookies rendering in view

…i don’t know what to do! obviously, this is probably more simple than
what i’m going through now(at least i can say i dear hope so) but i’m
having a terrible time trying to use cookies[:something]…

i have already implement a nice working cute forum, and i was hoping to
put the client/forum-user’s username and email into the corresponding
<input type=“text” … value="<%= @cookies[“username”]%>" /> fields.
my controller is defined to set the cookie:

def …
message = Message.find(params[:id])
cookies[“username”] = message.username.to_s

and after a while (after changing a cookies[:username] to
cookies[“username”] and @cookies[:username] and the likes, seems like a
random choice of symbol/paranthesies) i actually got it rendered into my
view, but the problem now, is that when it is rendered, it is viewed
like :

;=username=THE CORRECT USERNAME and the email is rendered kinda weird
also with the ‘@’ turned into %40 and a =path leading it…

is this supposed to be happening?? why isn’t it displayed without the
“=username=” and =path and ‘%40’ and all of that; what is h a p e n i n
g ? ? ?)


you probably shouldn’t be using cookie/session data directly in the
view, that should be the controllers responsibility.

simply put this in your controller code:
@username = cookies[:username]

if you want several fields from the cookies, you could do:

@formdata =
[ :username, :somefield, :anotherfield ].each do |cookieentry|
@formdata[cookieentry] = cookies[cookieentry]

now, @formdata would be accessible in the view, you could do: <%=
@formdata[:username] %>

also, cookies[:username] (without the @) in the view wouldn’t work, it’s
not a local variable or method on the view object. rails makes session,
cookie and flash data available to the view by putting them in variables
named @session, @cookies and @flash.

it works!

thanks…is there a place i could get more information on why it doesn’t
accept the cookie information as it is? why does it have to be delt with
via the controller first…either way, both @cookie and @formdata are
both hashes…no?

either way, i much appreicate the help.


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