Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value from header into model

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


1 Answers

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
like image 163
Thilo Avatar answered Feb 25 '26 02:02

Thilo