代码之家  ›  专栏  ›  技术社区  ›  noobmaster69

JSONAPI资源:NameError找不到资源

  •  1
  • noobmaster69  · 技术社区  · 3 年前

    我正在使用JSONAPI资源API项目。当我尝试命名它时,我遇到了一个错误,服务器无法启动。当我跑步时挖得更深 bin/rails zeitwerk:check 我得到以下错误:

    NameError: JSONAPI: Could not find resource 'api/v1/connectors'. (Class Api::V1::ConnectorResource not found)
    /Users/David/Code/Ruby/my_project/config/routes.rb:7:in `block (3 levels) in <main>'
    /Users/David/Code/Ruby/my_project/config/routes.rb:6:in `block (2 levels) in <main>'
    /Users/David/Code/Ruby/my_project/config/routes.rb:5:in `block in <main>'
    /Users/David/Code/Ruby/my_project/config/routes.rb:3:in `<main>'
    /Users/David/Code/Ruby/my_project/config/environment.rb:5:in `<main>'
    /Users/David/Code/Ruby/my_project/bin/rails:5:in `<top (required)>'
    /Users/David/Code/Ruby/my_project/bin/spring:10:in `block in <top (required)>'
    /Users/David/Code/Ruby/my_project/bin/spring:7:in `<top (required)>'
    Tasks: TOP => zeitwerk:check => environment
    (See full trace by running task with --trace)
    

    只需按照指南添加一个同名资源即可。如果我去掉命名,它就像一个符咒。

    连接器_控制器.rb

    module Api
      module V1
        class ConnectorsController < ApplicationController
        end
      end
    end
    

    连接器资源.rb

    module Api
      module V1
        class ConnectorResource < JSONAPI::Resource
          attributes :name, :description, :save_path, :created_at, :updated_at
        end
      end
    end
    

    路线.rb

    Rails.application.routes.draw do
      # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
      namespace :api do
        namespace :v1 do
          jsonapi_resources :connectors, only: %i[index show]
        end
      end
    end
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Noor Ahmed Biswas    3 年前

    我认为你把资源放错地方了,资源的实际路径应该在

    resouces/api/V1/some_recource.rb