Problem with reading cgi cookie

Why doesn’t this work in a controller, or in a file in a module in /lib:

require ‘cgi’
cgi = CGI.new
test = cgi.cookies[“mycookie”][0]

cgi.cookies doesn’t get the cookie.

But this cgi script does:

require ‘cgi’
cgi = CGI.new(‘html4’)
cookie_name = ‘mycookie’
val_string = cgi.cookies[cookie_name][0]
cgi.out {
cgi.html {
cgi.head { cgi.title {“Cookies”} } +
cgi.body {
cgi.p {“name: #{cookie_name}” } +
cgi.p {“value: #{val_string}”}
}
}
}

On Jul 9, 10:44 am, Pål Bergström [email protected] wrote:

Why doesn’t this work in a controller, or in a file in a module in /lib:

require ‘cgi’
cgi = CGI.new
test = cgi.cookies[“mycookie”][0]

Rails doesn’t use CGI anymore as the interface between it and the
outside world (it uses rack instead) which probably broke that.

Controllers have an instance method called cookies which returns a
hash like object containing your cookies.

Fred

Frederick C. wrote:
s doesn’t use CGI anymore as the interface between it and the

outside world (it uses rack instead) which probably broke that.

Controllers have an instance method called cookies which returns a
hash like object containing your cookies.

Fred

I see. Strange that it works on my machine locally.

I regularly use rails cookie instance in my rails app. But I can’t use
it in a module in a file in /app/lib, right? That’s why I used cgi.

The thing is, before save and show I encrypt certain fields in the
database using the gem fast_aes, and the key is in a cookie. In each
model I use before_save and after_find. How can I get the value of that
cookie without using cgi?

Frederick C. wrote:

uses rack

How do I read a cookie with rack? Trying to find the answer through
Google but witout luck.

Peter De Berdt wrote:

On 09 Jul 2010, at 13:56, Pål Bergström wrote:

This should help:

rack-cookie-monster/lib/rack/cookie_monster.rb at master · jdewind/rack-cookie-monster · GitHub

It did. Just a stupid question :slight_smile:

How do I use env?

On 09 Jul 2010, at 13:56, Pål Bergström wrote:

uses rack

How do I read a cookie with rack? Trying to find the answer through
Google but witout luck.

This should help:

Best regards

Peter De Berdt

Peter De Berdt wrote:

What do you mean? The environment is passed in with the required call
method from the rack middleware.

I don’t know. I just want to get a cookie value in a script in a file in
railsapp/lib. I thought this would do it:

req = Rack::Request.new(env)
req.cookies()

But I get “undefined local variable or method `env’”

On 09 Jul 2010, at 14:27, Pål Bergström wrote:

This should help:

rack-cookie-monster/lib/rack/cookie_monster.rb at master · jdewind/rack-cookie-monster · GitHub

It did. Just a stupid question :slight_smile:

How do I use env?

What do you mean? The environment is passed in with the required call
method from the rack middleware.

Best regards

Peter De Berdt

Peter De Berdt wrote:

On 09 Jul 2010, at 14:44, Pål Bergström wrote:

Well, to get started with middlewares, it’s a good thing to read up on
how Rails is using it:
http://guides.rails.info/rails_on_rack.html

Too much to get into now. And you script (thank you very much for you
effort :slight_smile: ) is much more than what I’ve used before with cgi.

require ‘cgi’
cgi = CGI.new
test = cgi.cookies[“mycookie”][0]

So, getting the value from a cookie into a model is that hard?

On 09 Jul 2010, at 14:44, Pål Bergström wrote:

But I get “undefined local variable or method `env’”
Well, to get started with middlewares, it’s a good thing to read up on
how Rails is using it:
http://guides.rails.info/rails_on_rack.html

In your case, something like this would probably do the trick:

That’s the basic structure. How you handle those cookies and what you
want to do with it will determine how you pass it on into the Rails
app. Google should be able to help you out there, since there’s lots
of middlewares out there that will do something similar to what you
want to do.

Best regards

Peter De Berdt

On 09 Jul 2010, at 16:26, Pål Bergström wrote:

I would just like to get the value of a particular cookie in a file in
/lib. Is there any other way without rack?

Why are you overcomplicating things so much? Cookies are part of the
request/response cycle and have nothing to do with models (MVC
remember, separation of logic). If you want to pass controller data
into a model or a custom class, you pass it as a parameter. In case of
your /lib file (which is a custom class I presume):

class FooBar

def initialize(mycookievariable)
# do something with mycookievariable
end

def somemethod
# etc.
end

end

and in your controller:

def show
my_foo_bar = FooBar.new(cookies[:mycookievariable)
end

Best regards

Peter De Berdt

I would just like to get the value of a particular cookie in a file in
/lib. Is there any other way without rack?

Peter De Berdt wrote:

Why are you overcomplicating things so much?

Good question. :slight_smile: It’s so much easier when you have a “programmers”
brain. I don’t.

Thanks for your suggestion. But how do I get the value of a cookie in
the model?

On 09 Jul 2010, at 17:02, Pål Bergström wrote:

Why are you overcomplicating things so much?

Good question. :slight_smile: It’s so much easier when you have a “programmers”
brain. I don’t.

Thanks for your suggestion. But how do I get the value of a cookie in
the model?

You pass it as a parameter to the model method, just like I passed it
into a custom class.

class FooBar < ActiveRecord::Base

def some_method(cookievalue)
   # do something here
end

end

In the controller

FooBar.some_method(cookies[:myvalue])

Best regards

Peter De Berdt

Peter De Berdt wrote:

On 09 Jul 2010, at 17:02, Pål Bergström wrote:

… or if you just want to make it a field value:

That was smart. Thanks. :slight_smile:

On 09 Jul 2010, at 17:02, Pål Bergström wrote:

Why are you overcomplicating things so much?

Good question. :slight_smile: It’s so much easier when you have a “programmers”
brain. I don’t.

Thanks for your suggestion. But how do I get the value of a cookie in
the model?

… or if you just want to make it a field value:

In controller

def create
   MyModel.create(params[:my_model].merge(:some_field =>

cookies[:some_cookie]))
end

No need to juggle around and just mass-assign using a hash, like
demonstrated above.

Best regards

Peter De Berdt

Pål Bergström wrote:

I still have problem. I just want to read a damn cookie. Why doesn’t
this work?

def create
@user = User.new(params[:user])
cookies[:user_name] = “david”


end

Sets a :user_name cookie with value “david” upon creating a user

def index
@users = User.all
@cookie_count = cookies.size
@user_name_cookie = cookies[:user_name] if @cookie_count > 0


end

Reads the :user_name cookie when listing users.

There are <%= @cookie_count %> cookies in request.

The :user_name cookie value is: <%= @user_name_cookie %>

Show the number of cookies and the value of the :user_name cookie
(“david”).

Note: This is a useless contrived example showing use of the “cookies”
method, but notice once you have the cookie value you can do whatever
you want with it, including passing it into a model.

Robert W. wrote:

def create
@user = User.new(params[:user])
cookies[:user_name] = “david”


end

Sets a :user_name cookie with value “david” upon creating a user

def index
@users = User.all
@cookie_count = cookies.size
@user_name_cookie = cookies[:user_name] if @cookie_count > 0


end

Reads the :user_name cookie when listing users.

There are <%= @cookie_count %> cookies in request.

The :user_name cookie value is: <%= @user_name_cookie %>

Show the number of cookies and the value of the :user_name cookie
(“david”).

I don’t want to set a cookie. That’s never been a problem. I would like
to read a cookie and get the value into models so I can access it when
using a before_save.

Pål Bergström wrote:

req = Rack::Request.new(env)
req.cookies()

I still have problem. I just want to read a damn cookie. Why doesn’t
this work?

Pål Bergström wrote:

def index
@users = User.all
@cookie_count = cookies.size
@user_name_cookie = cookies[:user_name] if @cookie_count > 0


end
I don’t want to set a cookie. That’s never been a problem. I would like
to read a cookie and get the value into models so I can access it when
using a before_save.

I though I had made that clear. You read the cookie value as shown in
above index method of the controller then you pass that value to the
model object.

If you don’t know how to pass a variable to a method, then I’d suggest
learning something about Ruby before you try to write a Rails
application. I don’t know how to make this any more clear.