【发布时间】:2011-07-03 07:51:43
【问题描述】:
我正在使用 Ruby on Rails 3,我想将一些 URL 路由到一些 Rack 中间件。也就是说,如果用户尝试浏览http://<my_site_name>.com/api/user/1,系统应该考虑在 Rack 文件之前运行,然后继续请求。
我有一个 Rack::Api:User 位于 lib/rack/api/user 文件夹中。
从 RoR 官方文档中我发现了这一点:
Mount a Rack-based application to be used within the application. mount SomeRackApp, :at => "some_route" Alternatively: mount(SomeRackApp => "some_route") All mounted applications come with routing helpers to access them. These are named after the class specified, so for the above example the helper is either +some_rack_app_path+ or +some_rack_app_url+. To customize this helper's name, use the +:as+ option: mount(SomeRackApp => "some_route", :as => "exciting") This will generate the +exciting_path+ and +exciting_url+ helpers which can be used to navigate to this mounted app.
在我尝试过的 routers.rb 文件中
mount "Rack::Api::User", :at => "/api/user/1"
# => ArgumentError missing :action
scope "/api/user/1" do
mount "Rack::Api::User"
end
# => NoMethodError undefined method `find' for "Rack::Api::User
我也试过了
match '/api/user/1' => Rack::Api::User
# => Routing Error No route matches "/api/user/1"
match '/api/user/1', :to => Rack::Api::User
# ArgumentError missing :controller
但没有人工作。
更新
我的 Rack 文件是这样的:
module Api
class User
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] =~ /^\/api\/user\/i
...
else
@app.call(env)
end
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 routing rack