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
In the controller home_controller.rb
In the view home\index.html.erb