Ruby on Rails 3을 사용하고 있으며 일부 URL을 일부 랙 미들웨어로 라우트하고 싶습니다. 즉, 사용자가 http://<my_site_name>.com/api/user/1
에서 찾아보기를 시도하면 시스템은 랙 파일보다 먼저 실행 한 다음 요청을 진행해야한다고 간주합니다.랙 라우팅 문제
lib/rack/api/user
폴더에있는 Rack :: Api : User가 있습니다. RoR에 공식 문서에서
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
tryed routers.rb 파일에서
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.
을
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
하지만, 아무도 아무도 없어. ks.
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
실제 사례를 보여줄 수 있습니까? 랙 파일이 '/ lib/api/user /'에 있다면, 어떻게 그리고 어디에서 '요구'해야합니까? P.S. : 'application.rb'파일 ('config.autoload_paths + = % W (# {config.root}/lib)'에 'lib'경로를로드하는 코드를 작성했습니다. 추신 II : 질문을 업데이트했습니다. 추신 : 귀하의 코드를 시도했지만 동일한 오류가 발생합니다. – user502052
내가 잘못하지 않았다면 Rack :: Api :: User를 자동로드하려면 클래스가'lib/rack/api/user.rb'에 정의되어 있어야합니다. 어쨌든'config/initializers/load_my_rack_app.rb' 파일을 생성하여 수동으로 파일을로드 할 수 있습니다 ('config/initializers'의 내용은 자동으로로드됩니다). lib/api/user/뭐든지 .rb ". –
하지만 '컨트롤러가 없습니다'라는 메시지가 나타납니다. 'user.rb'파일의 rack 문 때문입니까? – user502052