views:

156

answers:

1

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 %>
+1  A: 

There are a few ways to do this, I think the easiest/quickest way is:

@search = Event.search(params[:search] || Event::DEFAULT_SEARCH_PARAMETERS)

In event.rb

class Event < A:RB
  DEFAULT_SEARCH_PARAMETERS = {:state_is => 'NY', :foo => 'bar'} # set your defaults here
end

Having said that, I'm not sure how that's going to work with the checkboxes. You could also consider one of the following options:

Have a SearchSetting model which contains all the searchable parameters (you can also persist this for users if they can save searches, or just leave it disconnected). This will make your form much simpler.

Add an All Divisions checkbox which is checked by default. A little bit of javascript to manage the state of the checkboxes and a custom search method.

jonnii
I like your first approach. It seems like what I'm doing should be simple...just default a bunch of checkboxes...but I've been struggling.Giving your first suggestions a shot now.Thanks!!!
Danger Angell
Here's my DEFAULT_SEARCH_PARAMETERS statement:DEFAULT_SEARCH_PARAMETERS = { :division_like_any => ["0", "Sprint", "0", "Sport", "0", "Adventure", "0", "Expedition"] }Getting "uninitialized constant EventsController::DEFAULT_SEARCH_PARAMETERS".Any ideas?
Danger Angell
Where did you put the constant?
jonnii
I put it in event.rb
Danger Angell
Is there any attr_reader or cattr_reader in event.rb?
nanda
It's my bad, it should be Event::DEFAULT_SEARCH_PARAMETERS.
jonnii