一年没有为Rails玩游戏。
我只是想通过这样做来制作一个嵌套的API并得到一个未定义的方法`namespace'异常。我很确定这适用于Rail4和5.1
ActionController::RoutingError (uninitialized constant Api::V1::CalculationRecordsController):
Rails.application.routes.draw do
# resources :controllers
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
namespace :api do
namespace :v1, defaults: {format: 'json'} do
resources :calculation_records
end
end
end
class Api::V1::CalculationRecordController < ApplicationController
def index
@records = CalculationRecord.all
end
end
如果Rails再也没有假设这种语法,我会感到惊讶
class Api::V1::CalculationRecordController < ApplicationController
确保控制器文件名是calculation_records_controller.rb并将class Api::V1::CalculationRecordController
更改为class Api::V1::CalculationRecordsController
module Api::V1
class FilesController < ApplicationController
def index
render json: {message: "files"}
end
end
end
要么
class Api::V1::FilesController < ApplicationController
def index
@records = render json: {message: "files"}
end
end
和内部路线
resources :files, only: [:index]
确保维护确切的控制器名称