在 ruby​​ 和 json 请求中使用 RESTful API 进行全局搜索

问题描述

我正在根据客户端发送的 json 请求使用 ruby​​ 实现搜索端点,该请求应具有以下形式 GET /workspace/:id/searches? filter[query]=Old&filter[type]=ct:Tag,User,WokringArea&items=5 到目前为止,我添加搜索控制器,但我不确定如何将 json 请求中的参数提取到 ruby​​ 语法

class SearchesController < ApiV3Controller
    load_and_authorize_resource :workspace,class: "Company"
    load_and_authorize_resource :user,through: :workspace
    load_and_authorize_resource :working_area,through: :workspace

    def index
    end

    private

    def ability_klasses
      [WorkspaceAbility,UserWorkspaceAbility,WorkingAreaAbility]
    end
  end

解决方法

class SearchesController < ApiV3Controller

    load_and_authorize_resource :workspace,class: "Company"
    load_and_authorize_resource :user,through: :workspace
    load_and_authorize_resource :working_area,through: :workspace

    def index
      puts("filtered_params:",filtered_params)
      puts("Query parameter:" filtered_params[:query])
      puts("Type parameter:" filtered_params[:type])
      #Based on condition invoke the appropriate model
      if filtered_params[:type] == 'WorkspaceAbility'
        #search query
      end
    end

    private

    def filtered_params
      params.require(:filter).permit(:query,:type,:items)
    end
  end

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...