From: Brian Candler Date: 2010-09-04T19:33:40+09:00 Subject: Re: Read cookie P奪l Bergstr旦m wrote: > So I thought I would just ask: Can Ruby access a cookie in a > users browser? And I say again, that question is completely meaningless. Ruby is a programming language; it can do anything that a programming language can do. But by itself it knows nothing about cookies - they are an artefact of HTTP. So you need to be talking about a Ruby HTTP environment, such as Rails or Sinatra or Rack or Webrick, which deals with incoming HTTP requests. That's the "context" which someone else asked for. All of these do have mechanisms for accessing cookies - even if it's only directly accessing the HTTP Cookie: and Set-Cookie: headers - so the answer to your question is quite clearly "Yes". However, I imagine that "Yes" is not the answer you were looking for. What you actually have is a problem, and you want help finding a solution. In that case, you need to describe the specific problem. "I cannot access cookies in Rails" is not sufficient - clearly you can, and I showed you code which does it. "in Rails, I cannot access cookies inside a model object" is getting there. Or "I know how to access cookies inside the Rails controller but not in other objects" Even if you wrote some code in lib/foo.rb, this is still running in the context some Ruby HTTP framework; how you access cookies depends on what the framework provides. If it were running standalone it wouldn't receive a HTTP request in the first place (unless you were writing a HTTP server from scratch) > I could do it with cgi but not anymore. That also means nothing unless you expand on "it" (what you're trying to do), or the problem (you have some code which worked before but which does not work now, in which case you can post the code itself and ask for ways to rewrite it) In the very old days, I think Rails had a global CGI object for "the current request", since it was only ever intended to handle one request at a time within any particular process. Now that Rails apps can be multi-threaded, this will have been removed, as it wouldn't make any sense - each controller instance must have its own HTTP request object. They've also removed the use of the CGI library, since Rack does it better and faster. > I don't have the before_save and after_find in the controller. It's in > the model. Yes, it's part of the design of Rails that models have before_save and after_find hooks. Note that ActiveRecord, from which you have built your models, is also a part of Rails - albeit one which can be used outside of Rails quite easily. Regards, Brian. -- Posted via http://www.ruby-forum.com/.