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}"}
        }
    }
}

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 Cheung 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 Cheung wrote:

uses rack

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

This should help:

http://github.com/dewind/rack-cookie-monster/blob/master/lib/rack/cookie_monster.rb

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.

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'"

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:

http://gist.github.com/469445

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

Peter De Berdt 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

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?

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

… 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:

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:

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.

<p>There are <%= @cookie_count %> cookies in request.</p>
<p>The :user_name cookie value is: <%= @user_name_cookie %></p>

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 Walker 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.

<p>There are <%= @cookie_count %> cookies in request.</p>
<p>The :user_name cookie value is: <%= @user_name_cookie %></p>

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:

  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.

Robert Walker wrote:

Pål Bergström wrote:

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.

Sorry but I don't understand you. And I have a feeling you don't
understand me.

Let me ask you this instead; is there a way to get the value of a cookie
in a model?