Rails 5.2上的路由语法是否发生了很大变化

问题描述 投票:0回答:2

一年没有为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

controller

    class Api::V1::CalculationRecordController < ApplicationController
      def index
        @records = CalculationRecord.all
      end
    end

enter image description here

如果Rails再也没有假设这种语法,我会感到惊讶

ruby-on-rails ruby-on-rails-5
2个回答
1
投票
class Api::V1::CalculationRecordController < ApplicationController

确保控制器文件名是calculation_records_controller.rb并将class Api::V1::CalculationRecordController更改为class Api::V1::CalculationRecordsController


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]

确保维护确切的控制器名称

© www.soinside.com 2019 - 2024. All rights reserved.