简体   繁体   中英

pg_search using one text field and one category select-type field

Ruby on Rails newbie. I'm trying to use the pg_search gem but running into some difficulties (yes, i am looking at the documentation). I want a search form that has both a text field and a select field (category). Here is the code I have so far:

contact_info.rb

class ContactInfo < ActiveRecord::Base
...
include PgSearch
  pg_search_scope :search_by_category, (lambda do |category, query|
  raise ArgumentError unless [:prev, :cat].include?(category)
  {
      :against => category,
      :query => query
  }
  end
end

home_controller.rb

class HomeController < ApplicationController
  def index
    @contact_infos = ContactInfo.search_by_category(params[:query])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @contact_infos }
    end
end

home\\index.html.erb

<%= form_for :search_by_category, :url => {:controller => 'home', :action => 'index'},
  :html => {:method => 'get'} do |f| %>

  <%= text_field_tag :query %>
  <%= f.label :category_id, "Category" %>
  <%= f.collection_select :category_id, Category.all,
                        :id, :name,
                        {:prompt => 'All'} %>
  <br/>
  <%= f.submit "Search" %>
<% end %>

I'm getting this error:

ArgumentError in HomeController#index wrong number of arguments (1 for 2)

...

Request

Parameters:

{"utf8"=>"✓","query"=>"test","search_by_category"=>{"category_id"=>"2"},"commit"=>"Search"}

I understand that the problem is in this line:

@contact_infos = ContactInfo.search_by_category(params[:query])

but I'm not sure how exactly to pass both variables from the view (home\\index.html.erb).

Thanks!

OK - I figured it out. If it helps someone....

In the model contact_info.rb

class ContactInfo < ActiveRecord::Base
  include PgSearch

 ...
  pg_search_scope :search_by_category, :against => :previous_value
  scope :in_category, lambda { |category_id|
    where(:category_id => category_id)
  }
end

In the controller home_controller.rb

class HomeController < ApplicationController
  def index

  if params[:search_by_category].nil?
    @contact_infos = ContactInfo.search(params[:query])
  else
    tmp = params[:search_by_category]
    @contact_infos = ContactInfo.search_by_category(params[:query]).in_category(tmp[:category_id])
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @contact_infos }
    end
  end
 end
end

In the view home\\index.html.erb

...
    <%= form_for :search_by_category, :url => {:controller => 'home', :action => 'index'}, :html => {:method => 'get'} do |f| %>
    <%= text_field_tag :query %>
    <%= f.label :category_id, "Category" %>
    <%= f.collection_select :category_id, Category.all,
                            :id, :name,
                            {:prompt => 'All'} %>
    <br/>
    <%= f.submit "Search" %>
<% end %>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM