I have an index page of Users
that I want to filter with a search form using MetaSearch. However the values I want to search when I checkbox is clicked are stored as strings. For example, here's a form I want to apply MetaSearch to:
<% form_for(current_user.profile) do |f| %>
<table id="careerCriteria">
<tr>
<td class="normal"><%= current_user.profile.hometown %></td>
<td><%= check_box_tag :hometown %></td>
</tr>
<tr>
<td class="normal"><%= current_user.profile.current_city %></td>
<td><%= check_box_tag :current_city %></td>
</tr>
<tr>
<td class="normal"><%= current_user.profile.past_city %></td>
<td><%= check_box_tag :past_city %></td>
</tr>
</table>
<% end %>
My User model:
class User < ActiveRecord::Base
has_one :profile, :dependent => :destroy
end
I don't want to use a search button. I want the filter(s) to be applied when the checkbox (or checkboxes) are clicked. I'm new to programming so any help would be appreciated!
You'll need a little ajax & query to accomplish this.
Here's a good article to show you how to make the checkbox submit the form.
http://trevorturk.com/2010/08/24/easy-ajax-forms-with-rails-3-and-jquery/
What you'll want to do is create an action in the controller that handles your search. Here's an example of a search action...
def search
if params[:term].blank?
raise "You must provide search criteria."
end
params[:term] = "%#{params[:term]}%"
conditions = " Description LIKE :term"
@careers = Career.all(
:conditions => [conditions, params],
:offset => params[:offset],
:limit => params[:limit]
)
respond_with @careers
end
You'll also need to setup a route for this search for this action.
resources :careers do
get "search/:term/:offset/:limit.:format", :action => "search", :constraints => { :offset => /\d+/, :limit => /\d+/ }
end
Once you get the form submitting to this action you should be able to use jQuery to update the results.
Now keep in mind if you don't want to use Ajax & jQuery to load the results you can do so, you'll just take the remote action out of the form tag and it will refresh the whole page.