How do I set default search conditions with Searchlogic?

Posted by Danger Angell on Stack Overflow See other posts from Stack Overflow or by Danger Angell
Published on 2010-04-21T14:00:08Z Indexed on 2010/04/21 14:03 UTC
Read the original article Hit count: 362

Filed under:
|
|

I've got a search form on this page:

http://staging-checkpointtracker.aptanacloud.com/events

If you select a State from the dropdown you get zero results because you didn't select one or more Event Division (checkboxes).

What I want is to default the checkboxes to "checked" when the page first loads...to display Events in all Divisions...but I want changes made by the user to be reflected when they filter.

Here's the index method in my Events controller:

def index
  @search = Event.search(params[:search])

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @events }
  end
end

Here's my search form:

<% form_for @search do |f| %>

<div>
<%= f.label :state_is, "State" %> <%= f.select :state_is, ['AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY'], :include_blank => true %>
</div>

<div>
    <%= f.check_box :division_like_any, {:name => "search[:division_like_any][]"}, "Sprint", :checked => true %> Sprint (2+ hours)<br/>
    <%= f.check_box :division_like_any, {:name => "search[:division_like_any][]"}, "Sport" %> Sport (12+ hours)<br/>
    <%= f.check_box :division_like_any, {:name => "search[:division_like_any][]"}, "Adventure" %> Adventure (18+ hours)<br/>
    <%= f.check_box :division_like_any, {:name => "search[:division_like_any][]"}, "Expedition" %> Expedition (48+ hours)<br/>
</div>

<%= f.submit "Find Events" %>
<%= link_to 'Clear', '/events' %>
<% end %>

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails