我在rails应用中创建API时遇到错误。错误是:
ActionController::RoutingError (uninitialized constant
API::V1::PopularSchoolsController):
我的文件夹结构是:
我的路线:
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
答案 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