2013-02-07 4 views
2

Rails 3.2.11 앱에서 Grape API를 실행하려고합니다. 포도 문서에 따르면 다음 줄을 내 config/application.rb에 추가해야합니다. 아래 코드Rails 3.2.11 & Grape에서는 다음 파일을 찾을 수 없습니다. app/api/v1.rb

# Auto-load API and its subdirectories 
config.paths.add "app/api", :glob => "**/*.rb" 
config.autoload_paths += Dir["#{Rails.root}/app/api/*"] 

그리고 생성 설정/초기화/reload_api.rb :

if Rails.env.development? 
    api_files = Dir["#{Rails.root}/app/api/**/*.rb"] 
    api_reloader = ActiveSupport::FileUpdateChecker.new(api_files) do 
    Rails.application.reload_routes! 
    end 
    ActionDispatch::Callbacks.to_prepare do 
    api_reloader.execute_if_updated 
    end 
end 

하지만 나를 위해 작동하지 않습니다. 여기 내 코드가있다.

module Api 
    class V1 < Grape::API 

    get '/hello' do 
     [] 
    end 
    end 
end 

그리고 내 설정/routes.rb : 나는 rake routes을 실행하면

Blog::Application.routes.draw do 

    mount Api::V1 => "/" 
end 

내가받을 다음과 같은 오류 : 내 응용 프로그램/API/v1.rb에서

uninitialized constant Api 
/home/zoloo/code/nomin/config/routes.rb:7:in `block in <top (required)>' 
/home/zoloo/.rvm/gems/[email protected]/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec' 
/home/zoloo/.rvm/gems/[email protected]/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:282:in `eval_block' 
/home/zoloo/.rvm/gems/[email protected]/gems/actionpack-3.2.11/lib/action_dispatch/routing/route_set.rb:260:in `draw' 
/home/zoloo/code/nomin/config/routes.rb:5:in `<top (required)>' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `block in load_paths' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `each' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:40:in `load_paths' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:16:in `reload!' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:26:in `block in updater' 
/home/zoloo/.rvm/gems/[email protected]/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `call' 
/home/zoloo/.rvm/gems/[email protected]/gems/activesupport-3.2.11/lib/active_support/file_update_checker.rb:78:in `execute' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:27:in `updater' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing' 
/home/zoloo/code/nomin/config/environment.rb:5:in `<top (required)>' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application.rb:103:in `require' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application.rb:103:in `require_environment!' 
/home/zoloo/.rvm/gems/[email protected]/gems/railties-3.2.11/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks' 
Tasks: TOP => routes => environment 
(See full trace by running task with --trace) 

그것은 분명 예외는 Rails가 내 API를로드 할 수 없다고 말했습니다. 왜? 에

답변

7

변경에게 자동로드 경로 :

config.autoload_paths += Dir["#{Rails.root}/app"] 

시도를 필요로

는 처음 api_v1.rb라는 파일을 찾습니다 Api::V1를 해결합니다. 존재하지 않습니다. 그런 다음 모든 자동로드 경로에서 api/v1.rb을 찾습니다. 자동로드 경로에 app이 없지만 하위 폴더가 모두 app/api입니다. 위 코드를 사용하면 v1.rb을 에 넣어야합니다.

+0

대단히 감사합니다. :). 작동 중입니다 ........ – Zeck