路由语法在Rails 5.2上进行了很多更改

时间:2019-03-14 01:08:21

标签: ruby-on-rails ruby-on-rails-5

一年没来过Rails。

我只想通过做一个嵌套的API并得到一个未定义的方法“命名空间”异常。 我很确定这可以在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

enter image description here

如果Rails不再采用这种语法,我会感到惊讶

2 个答案:

答案 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]

确保您保留确切的控制器名称