Rails 3.1 - 使用控制器 after_filter 对我不起作用,所以我添加了一个自定义中间件:
在 app/middleware/cors_middleware.rb 中:
# For icons to work in Firefox with CDN
class CorsMiddleware
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
cors_headers = headers.merge({
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => '*'
})
[status, cors_headers, body]
end
end
在 config/application.rb 中:
require File.join(Rails.root, "app", "middleware", "cors_middleware")
config.middleware.insert_before ActionDispatch::Static, CorsMiddleware # Need it early in the chain to work for assets