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