未初始化的常量API :: V1 :: PopularSchoolsController - Rails API

时间:2017-01-10 12:55:13

标签: ruby-on-rails ruby web-services api ruby-on-rails-4

我在rails应用中创建API时遇到错误。错误是:

ActionController::RoutingError (uninitialized constant 
API::V1::PopularSchoolsController):

我的文件夹结构是:

enter image description here

我的路线:

namespace :api, defaults: {format: :json} do
    namespace :v1 do
      resources :popular_schools
    end
  end

控制器:

class API::V1::PopularSchool::PopularSchoolsController < ApplicationController

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

end

是inflections.rb:

ActiveSupport::Inflector.inflections(:en) do |inflect|
#   inflect.acronym 'RESTful'
    inflect.acronym 'API'
    inflect.acronym 'V1'
end

1 个答案:

答案 0 :(得分:1)

只需将控制器放在popular_school文件夹之外即可。比方说,进入v1文件夹。

或者通过在其中添加名称空间namespace :popular_school来更改路线,如下所示:

namespace :api, defaults: {format: :json} do
    namespace :v1 do
      namespace :popular_school do
       resources :popular_schools
      end 
    end
  end