【问题标题】:Rails api controller doesn't inherit method from the parent classRails api 控制器不从父类继承方法
【发布时间】:2020-01-14 18:03:25
【问题描述】:

为我的第一个 Rails 项目做 API。 我有所有 API 的基类 ApiController:

module Api
  class ApiController < ::ApplicationController
    respond_to :json
    skip_before_action :verify_authenticity_token

    def index
      @collection = resource_class.all
      render json: @collection.as_json(as_json_collection)
    end

    private

    def resource_class
      raise NotImplementedError
    end

    def as_json_collection
      {}
    end

  end
end

我有子类UsersController:

module Api
  class UsersController < ApiController

    private

    def resource_class
      User
    end

    def resource_params
      params.require(:user).permit(:name, :email)
    end
  end
end

我的路线:

namespace :api do
    resources :users
end

那我去 my_app/api/users 我有错误:

找不到 Api::UsersController 的操作“索引”

但后来我更改了 UsersController 编写它自己的索引类,一切正常,我的所有用户都采用 JSON 格式。 我已经尝试评论两个课程中的所有私人标记,但这无济于事。 我不想为我的项目中的每个实体编写一个 API,我希望以后避免这个问题。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我得到了它的工作:

    module Api
      class ApiController < ::ApplicationController
    
        def index
          respond_to do |format|
            format.json { render json: '"abc"' }
          end
        end
      end
    end
    
    module Api
      class UsersController < ApiController
      end
    end
    

    网址是http://localhost:3000/api/users.json

    所以我建议你:

    module Api
      class ApiController < ::ApplicationController
    
        def index
          respond_to do |format|
            format.json do
              @collection = resource_class.all
              render json: @collection.as_json(as_json_collection)
            end
          end
        end
      end
    end
    
    module Api
      class UsersController < ApiController
        def resource_class
          User
        end
    
        def resource_params
          params.require(:user).permit(:name, :email)
        end
      end
    end
    

    【讨论】:

    • 照你说的做了。而且似乎现在 rails 可以看到我的 index 操作,这很酷。但是现在我在 ApiController 类的respond_to do |format| 行中有 ActionController::UnknownFormat 错误。
    • 我编辑了我的答案,因为我忘记了format.json。确保 URL 末尾有 .json
    • 我不知道respond_to 是否是你的问题,但我的第一个例子可以工作,所以继承工作。您必须从继承工作中取出它并将功能代码放回原处。
    • 现在可以了,谢谢。如果我使用您的示例,我应该记得在 URL 中添加 .json
    【解决方案2】:

    应该是这样的:

      class Api::ApiController < ApplicationController
    

    别忘了删除多余的end,文件结尾!

    #sample
    - api(folder)
    -- api_controller.rb   (Api::ApiController < ApplicationController)
    -- users_controller.rb (Api::UsersController < Api::ApiController)
    
    application_controller.rb
    

    【讨论】:

    • 另外,您在users_controller 中没有index 操作,请创建它!!!
    • 如果我在父类中有索引操作,为什么要添加它?还是不行
    • @Arty 因为这个my_app/api/users 询问用户索引方法不是父级。
    • 如果想让父母成为一个:get 'index' =&gt; 'api#index',路由my_app/api/index
    • @7urkm3n 继承的工作方式,如果类没有,它应该问父类。
    【解决方案3】:

    你需要阅读我的朋友: rails routes

    当你这样做时:

    namespace :api do
        resources :users
    end
    

    rails 自动创建 CRUD 路由,这意味着 my_app/api/users 将转换为:.../users#index

    执行此操作以查看您由 rails 创建的路线:

    rails routes 和特定词(例如用户):rails routes | grep user

    眼见为实;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 2016-08-14
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多