【问题标题】:Global search with RESTful API in ruby and json requests在 ruby​​ 和 json 请求中使用 RESTful API 进行全局搜索
【发布时间】:2021-02-10 19:21:56
【问题描述】:

我正在根据客户端发送的 json 请求使用 ruby​​ 实现搜索端点,该请求应具有以下形式 获取 /workspace/:id/搜索? filter[query]=旧&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

【问题讨论】:

    标签: ruby-on-rails json restful-url rails-api


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 2013-11-16
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      相关资源
      最近更新 更多