2017-02-21 78 views
0

我试图在Rails 5网站上实现高级搜索。用户传入参数“provider_type”,并且我想返回包含该值的所有记录。该值是使用简单形式从下拉列表中选择的。我new.html.erb看起来是这样的:在postgres列中搜索参数值

<%= simple_form_for Search.new, :html => {:class => 'form-horizontal' } do |f| %> 
    <%= f.input :provider_type, collection: ['Mental Health', 'Medical'] %> 
    <%= f.button :submit %> 
<% end %> 

我的搜索模式是这样的:

class Search < ApplicationRecord 
    def search_providers 
     providers = Provider.all 
     providers = providers.where("provider_type LIKE ?", ['Mental Health', 'Medical']) if provider_type.present? 
     providers 
    end 
end 

我的搜索器:

def SearchesController < ApplicationController 
    def new 
     @types = Provider.uniq.pluck(:provider_type) 
    end 

    private 
     def search_params 
      params.require(:search).permit(:provider_type) 
     end 
    end 
end 

当我尝试搜索“心理健康“的搜索形式,我得到这个错误:PG :: UndefinedFunction:错误:操作符不存在:字符变化[] ~~未知

EDIT

当我改写它作为

providers.where(provider_type: provider_type) if provider_type.present? 

这将产生错误“PG :: InvalidTextRepresentation:错误:畸形的数组文本: “%心理健康%” DETAIL:数组值MUT开始与 “{”或尺寸信息。

回答