【发布时间】:2010-11-19 12:22:25
【问题描述】:
大家好, 我正在尝试使机架中间件NotFound 与 rails3 一起使用,但我需要进行一些更改以返回一些 json,所以我基本上定义了一个新类:
class NotFound
def initialize(app, msg, content_type = "text/html")
@app = app
@content = msg
@length = msg.size.to_s
@content_type = content_type
end
def call(env)
[404, {'Content-Type' => @content_type, 'Content-Length' => @length}, @content]
end
end
我将上面的这个类添加到“app/middleware/not_found.rb”中,并将下面的这一行添加到我的 application.rb 文件中:
config.middleware.use "NotFound", {:error => "Endpoint Not Found"}.to_json, "application/json"
现在......好吧,它按我的预期工作......它总是返回
{"error"=>"Endpoint Not Found"}
现在我怎样才能让它只有在路由器出现故障时才能工作?我看到有一个 insert_after 方法,但在 Application.routes 之后无法实现
ps : 我知道我可以用 rails3 路由器处理它,但这是一个实验,我只是玩得开心:-)
谢谢!
【问题讨论】:
-
为什么不使用 Rails 系统。小东西比较复杂。
-
你应该阅读整个问题,我的意思是直到最后......“ps:我知道我可以用 rails3 路由器处理它,但这是一个实验,我只是玩得开心”
标签: ruby-on-rails ruby-on-rails-3 rack