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