Skip to content

Instantly share code, notes, and snippets.

@sofadesign
Created January 22, 2011 15:47
Show Gist options
  • Save sofadesign/791187 to your computer and use it in GitHub Desktop.
Save sofadesign/791187 to your computer and use it in GitHub Desktop.
des Headers HTTP personnalisés dans Rails
# des Headers HTTP personnalisés dans Rails:
class MyController
before_filter :custom_response_headers
def custom_response_headers
response.headers["X-Luna-Mood"] = ";-)"
# …
end
end
# Ou mieux, sous forme de middleware Rack:
class LunaInfos
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
headers["X-Luna-Mood"] = ";-)"
# …
[status, headers, body]
end
end
# Que l'on déclare dans l'initializer de rails
Rails::Initializer.run do |config|
config.middleware.use "LunaInfos"
end
# Pour en savoir plus:
# * <http://guides.rubyonrails.org/rails_on_rack.html>
# * <http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/>
# * <http://asciicasts.com/episodes/151-rack-middleware>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment