一年没来过Rails。
我只想通过做一个嵌套的API并得到一个未定义的方法“命名空间”异常。 我很确定这可以在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不再采用这种语法,我会感到惊讶
答案 0 :(得分:1)
class Api::V1::CalculationRecordController < ApplicationController
确保控制器文件名是Calculation_records_controller.rb,并将class Api::V1::CalculationRecordController
更改为class Api::V1::CalculationRecordsController
答案 1 :(得分:0)
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]
确保您保留确切的控制器名称