Я использую 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