简体   繁体   English

Rails api 控制器不从父类继承方法

[英]Rails api controller doesn't inherit method from the parent class

Doing API for my first Rails project.为我的第一个 Rails 项目做 API。 I have base class ApiController for all the APIs:我有所有 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

And I have child class UsersController:我有子类 UsersController:

module Api
  class UsersController < ApiController

    private

    def resource_class
      User
    end

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

My routes:我的路线:

namespace :api do
    resources :users
end

Then I'm going to my_app/api/users I have error:然后我要去 my_app/api/users 我有错误:

The action 'index' could not be found for Api::UsersController找不到 Api::UsersController 的操作“索引”

But then I changing UsersController writing it's own index class, everything works fine and I'm having all my Users in JSON format.但是后来我更改了 UsersController 编写它自己的索引类,一切正常,并且我的所有用户都采用 JSON 格式。 I've alrady tried to comment all private marks in both classes, but that doesn't help.我已经尝试评论两个班级中的所有私人标记,但这无济于事。 I don't want to write an API for every entity in my project and I'd like to avoid this problem in future.我不想为我的项目中的每个实体编写 API,我希望将来避免这个问题。

I got it to work with this:我让它与这个一起工作:

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

The URL was http://localhost:3000/api/users.json URL 是http://localhost:3000/api/users.json

So for you I suggest:所以对于你,我建议:

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

Its supposed to be like this:它应该是这样的:

  class Api::ApiController < ApplicationController

and do not forget to remove extra end , end of the file!并且不要忘记删除额外的end ,文件的结尾!

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

application_controller.rb

You need to read this my friend: rails routes你需要阅读我的朋友: rails routes

When you do this:当你这样做时:

namespace :api do
    resources :users
end

rails creates CRUD routes automatically which means that my_app/api/users will translate to: .../users#index . rails 会自动创建 CRUD 路由,这意味着my_app/api/users将转换为: .../users#index

Do this to see your routes created by rails:执行此操作以查看由 rails 创建的路由:

rails routes and for specific word (eg user): rails routes | grep user rails routes和特定词(例如用户): rails routes | grep user rails routes | grep user

Seeing is believing ;)眼见为实 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM