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

前端 未结 1 1587
不思量自难忘°
不思量自难忘° 2021-02-06 12:33

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

相关标签:
1条回答
  • 2021-02-06 12:50

    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 讨论(0)
提交回复
热议问题