2016-05-08 20 views
5

Tôi đang tạo một điểm cuối api đơn giản trong ứng dụng Rails 4.2.6 của mình nhưng đang gặp sự cố với ứng dụng này.Không thể tự động tải Bộ điều khiển API liên tục trong Rails 4

Khi tôi nhấn url: http://lvh.me:9077/api/v1/grubs tôi nhận được lỗi sau:

Unable to autoload constant Api::V1::GrubsController, expected /Users/shakycode/code/grubs/app/controllers/api/v1/grubs_controller.rb to define it 

Dưới đây là tập tin routes.rb tôi xác định điểm cuối.

namespace :api do 
    namespace :v1 do 
     resources :grubs, only: [:index] 
    end 
    end 

Đây là tôi app/controllers/api/v1/grubs_controller.rb

class API::V1::GrubsController < ApplicationController 
    protect_from_forgery with: :null_session 
    before_action :destroy_session 

def destroy_session 
    request.session_options[:skip] = true 
end 

    def index 
    @grubs = Grub.all 
    respond_to do |format| 
     format.json { render json: @grubs} 
    end 
    end 
end 

Tôi có một ứng dụng Rails 4.2.1 nơi tôi sử dụng chiến lược tương tự, nhưng trong 4.2.6 tôi m có lỗi này xảy ra khi tôi cố gắng chống lại API.

Cảm ơn trước!

Cập nhật: Đây là ngoại lệ mà được nâng lên bằng better_errors trong trình duyệt:

load_missing_constantactivesupport (4.2.6) lib/active_support/dependencies.rb 
490 
491 
492 
493 
494 
495 
496 
497 
498 
499 
500 
     if loading.include?(expanded) 
      raise "Circular dependency detected while autoloading constant #{qualified_name}" 
     else 
      require_or_load(expanded, qualified_name) 
      raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false) 
      return from_mod.const_get(const_name) 
     end 
     elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) 
     return mod 
     elsif (parent = from_mod.parent) && parent != from_mod && 

Trả lời

9

Rails thường chỉ viết hoa tên đầu tiên của một mô-đun. Nói cách khác, Rails hy vọng không gian tên Api::V1::GrubsController, nhưng bạn đang xác định nó là API::V1::GrubsController.

+0

Điều này! Nó thậm chí còn nói 'Không thể tự động nạp liên tục Api :: V1 :: GrubsController, dự kiến ​​/Users/shakycode/code/grubs/app/controllers/api/v1/grubs_controller.rb để xác định nó'. Bởi vì 'grubs_controller.rb' là định nghĩa một' API :: V1 :: GrubsController', nhưng không phải là một 'Api :: V1 :: GrubsController'. – fbelanger

2

tên lớp của bạn là

class API::V1::GrubsController < ApplicationController 

trong khi do lỗi của bạn mình cố gắng để tìm kiếm Api::V1::GrubsController. Thay đổi tên trong lớp của bạn thành Api

+0

OK, tôi đã bỏ lỡ phần đó trên API so với Api. – nulltek

Các vấn đề liên quan