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 should use request. original_url to get the current URL.
Models are used to represent data from the database (rows of data). The hash representing a row of data can be used to read and update that row's associated data.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With