开发者

How to get the actual HTTP request from a model in rails 3?

I would need to get the actual Request from a model in rails 3. I know there is not always an request processed, but if there is one I 开发者_如何转开发would like to be able to access it. How is it possible to reach. In case of controllers, the request method is in place. But now I need to access the request from a bit "lower level". Can you give me any clues how to do it in Rails?


You can store the request in the thread, and then access it anywhere. This is def a hack as you really should not break the MVC convention this way, and if a model is really request dependent you could always pass the request to the model as a parameter.

but the hack to make your request available everywhere would be for application_controller.rb:

before_filter :store_request_in_thread

def store_request_in_thread
  Thread.current[:request] = request
end

and in your model somemodel.rb or really anywhere you expect request to already exist, you can just access the request:

def something
  request = Thread.current[:request]
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜