2011-02-24 7 views
1

Ruby on Rails 3을 사용하고 있으며 일부 URL을 일부 랙 미들웨어로 라우트하고 싶습니다. 즉, 사용자가 http://<my_site_name>.com/api/user/1에서 찾아보기를 시도하면 시스템은 랙 파일보다 먼저 실행 한 다음 요청을 진행해야한다고 간주합니다.랙 라우팅 문제

lib/rack/api/user 폴더에있는 Rack :: Api : User가 있습니다. RoR에 공식 문서에서

내가이 발견 : 나는 또한 tryed

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 

답변

3

당신은 초기화에서처럼, 당신의 부팅 과정에서 어딘가에 랙 응용 프로그램을 -ing require있어 가정 (:


UPDATE

내 랙 파일은 다음과 같은 것입니다 lib의 파일은 코드를 작성하지 않으면 더 이상 자동으로로드되지 않습니다 (this SO answer for more 참조). 따옴표없이 마운트를 시도하십시오. 대신 예를 들어

mount "Rack::Api::User", :at => "/api/user/1" 

시도

mount Rack::Api::User, :at => "/api/user/1" 
[업데이트] 여기

내가 자동 로딩 및 설치를 모두 보여줍니다 기본 Rails 애플리케이션에 대한 변경 사항에 대한 링크가

랙 응용 프로그램 : https://github.com/BinaryMuse/so_5100999/compare/master...rack

[업데이트 2]

아, 네가 지금 무슨 말하는지 알 겠어. 미들웨어가 필요합니다. 위 URL에서 코드를 업데이트하여 애플리케이션을 미들웨어로 구현했습니다. config/initializers/rack.rb은 미들웨어를로드하고 삽입하는 파일입니다. 희망이 당신이 찾고있는 것입니다!

+0

실제 사례를 보여줄 수 있습니까? 랙 파일이 '/ lib/api/user /'에 있다면, 어떻게 그리고 어디에서 '요구'해야합니까? P.S. : 'application.rb'파일 ('config.autoload_paths + = % W (# {config.root}/lib)'에 'lib'경로를로드하는 코드를 작성했습니다. 추신 II : 질문을 업데이트했습니다. 추신 : 귀하의 코드를 시도했지만 동일한 오류가 발생합니다. – user502052

+0

내가 잘못하지 않았다면 Rack :: Api :: User를 자동로드하려면 클래스가'lib/rack/api/user.rb'에 정의되어 있어야합니다. 어쨌든'config/initializers/load_my_rack_app.rb' 파일을 생성하여 수동으로 파일을로드 할 수 있습니다 ('config/initializers'의 내용은 자동으로로드됩니다). lib/api/user/뭐든지 .rb ". –

+0

하지만 '컨트롤러가 없습니다'라는 메시지가 나타납니다. 'user.rb'파일의 rack 문 때문입니까? – user502052