I am wondering is there a way get value from header directly on model.
# location.rb
class Location < ActiveRecord::Base
puts request.header['key']
end
Let's pretend we have some value stored into header, into controller I use request.headers['key']. it worked but into model it's not
As the comments point out, no, not directly. The request object is only available in the controller.
But since you have to instantiate your model in the controller anyway, there should be no problem in doing something like this:
# some_controller.rb
def some_action
@location = Location.new(name: request.headers['key'])
...
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