我已经有一年没玩过Rails了。
我只是想通过这样做来制作一个嵌套的API,并得到了一个未定义的方法‘`namespace’异常。我非常确定这在Rail4和5.1上是有效的
ActionController::RoutingError (uninitialized constant Api::V1::CalculationRecordsController):
route.rb
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不再支持这种语法,我会很惊讶的。
发布于 2019-03-14 11:49:46
class Api::V1::CalculationRecordController < ApplicationController
确保控制器文件名为calculation_records_controller.rb,并将class Api::V1::CalculationRecordController
更改为class Api::V1::CalculationRecordsController
发布于 2019-03-14 12:26:18
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]
确保您维护了准确的控制器名称
https://stackoverflow.com/questions/55153455
复制