简体   繁体   中英

uninitialized constant API::V1::PopularSchoolsController - Rails API

I am getting error while creating API in rails app. The error is:

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

My folder structure is:

在此处输入图片说明

My routes:

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

Controller:

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

Just put your controller outside popular_school folder. Say that, into v1 folder.

Or change your routes by adding namespace namespace :popular_school in them like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM