问题描述
我正在根据客户端发送的 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