get 请求中的限制参数在 Rails 5 Grape Api 中不起作用

Limit parameter in get request not working in Rails 5 Grape Api

我构建了我的第一个 api,它按预期工作,但我很难理解为什么我不能在我的请求中限制结果。我在主题上看到的所有搜索都列出了与我在下面列出的相同的限制参数。我的 api 是用 Grape 构建的 Rails 5. 知道我做错了什么吗?

https://www.soledadmemorial.com/api/v1/plaques?limit=10

controllers/api/v1/plaques.rb

module API  
    module V1
      class Plaques < Grape::API
        include API::V1::Defaults

        resource :plaques do
          desc 'Return all Plaques'
          get '', root: :plaques do
             Plaque.all
          end

          desc 'Return a Plaque'
          params do
            requires :id, type: String, desc: 'ID of Plaque'
          end
          get ':id', root: 'plaque' do
            Plaque.where(id: permitted_params[:id]).first!
          end
        end
      end
    end
  end 

plaques_controller.rb

class PlaquesController < ApplicationController
  before_action :authenticate_user!, :except => [:index, :show]
  before_action :set_plaque, only: [:show, :edit, :update, :destroy]

  # GET /plaques
  # GET /plaques.json
  def index
    @plaquelist = Plaque.search(params[:search]).paginate(:per_page => 10, :page => params[:page])
    @plaques = Plaque.all
  end

  # GET /plaques/1
  # GET /plaques/1.json
  def show
    @plaques = Plaque.all
  end

  # GET /plaques/new
  def new
    @plaque = Plaque.new
  end

  # GET /plaques/1/edit
  def edit
  end

  # POST /plaques
  # POST /plaques.json
  def create
    @plaque = Plaque.new(plaque_params)

    respond_to do |format|
      if @plaque.save
        format.html { redirect_to @plaque, notice: 'Plaque was successfully created.' }
        format.json { render :show, status: :created, location: @plaque }
      else
        format.html { render :new }
        format.json { render json: @plaque.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /plaques/1
  # PATCH/PUT /plaques/1.json
  def update
    respond_to do |format|
      if @plaque.update(plaque_params)
        format.html { redirect_to @plaque, notice: 'Plaque was successfully updated.' }
        format.json { render :show, status: :ok, location: @plaque }
      else
        format.html { render :edit }
        format.json { render json: @plaque.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /plaques/1
  # DELETE /plaques/1.json
  def destroy
    @plaque.destroy
    respond_to do |format|
      format.html { redirect_to plaques_url, notice: 'Plaque was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_plaque
    @plaque = Plaque.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def plaque_params
    params.require(:plaque).permit(
        :veteran_war,
        :veteran_biography,
        :veteran_first,
        :veteran_last,
        :veteran_display_name,
        :group_type,
        :veteran_nickname,
        :group_name,
        :veteran_middle,
        :veteran_rank,
        :veteran_branch,
        :grid, :wall,
        :direction,
        :row,
        :plaque_num,
        :image,
        :search
    )
  end
end

我不是 100% 确定它正在执行哪个操作 - 但现在假设索引:

def index
  @plaquelist = Plaque.search(params[:search]).paginate(:per_page => 10, :page => params[:page])
  @plaques = Plaque.all
  return unless params[:limit]
  @plaquelist = @plaquelist.limit(params[:limit])
  @plaques = @plaques.limit(params[:limit])
end

我没有测试过,但理论上如果有一个限制参数(在你给的 URL 上有)那么它将采用该限制并将其应用于你要返回的对象.目前您的控制器没有对 limit 参数执行任何逻辑,即使它存在也是如此。

如果没有限制参数,此逻辑将 'return' 在您的代码之后,如果有限制参数,则会更改控制器给出的结果。

编辑:也可以稍微整理一下,但会把有趣的部分留给你 :D