Created
January 22, 2011 15:47
-
-
Save sofadesign/791187 to your computer and use it in GitHub Desktop.
des Headers HTTP personnalisés dans Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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