f.checkbox?

Okay so I have a typical search page where a keyword is entered and
results are retrieved that match the keyword. What I want to do now is
have checkboxes to search for results in specific areas (my site is a
big events calendar with "streams" and "audiences") I want to use the
checkboxes to look for results in 'streams', 'audiences', or both. I
want to be able to return either true or false from a method depending
on whether or not a box is checked. I have no idea how to do this, I've
only used checkboxes with a database before, and I don't want to use
columns in a database for this.

This is my setup so far:

in search view:

[code=]<% form_for :event do |f| %>
  <label for="stream_search">Search Streams</label> <%= f.check_box
:search_streams, {:class=> "check"} %><br />
  <label for="aud_search">Search Audiences</label> <%= f.check_box
:search_auds, {:class=> "check"} %> <br />
<% end %>[/code]

and I want to be able to somehow get these methods in the event model to
return either true or false depending on the checkboxes so I can use
them in if-statements in my search method:
[code=] def search_streams
  true

  end

  def search_auds
  true

  end[/code]

Thanks in advance for any help!

[code=]<% form_for :event do |f| %>
  <label for="stream_search">Search Streams</label> <%= f.check_box
:search_streams, {:class=> "check"} %><br />
  <label for="aud_search">Search Audiences</label> <%= f.check_box
:search_auds, {:class=> "check"} %> <br />
<% end %>[/code]

if you use them this way, Rails may complain about missing columns,
since the form _for is bound to the events model.
You could use simple check_box_tag here:

<%= check_box_tag("Search Streams", "search_streams") %>

or
<%= check_box_tag("Search Streams", "event[search_streams]") %>

the second version would (if I got the syntax right) add the param to
params[:event] instead creating it's own.

the return value would be in params[:search_streams] and be "0" for
false or "1" for checked. (As a string, not number)

So you could do
if params[:search_streams] == "1" then
...
end

Thorsten Mueller wrote:

if you use them this way, Rails may complain about missing columns,
since the form _for is bound to the events model.
You could use simple check_box_tag here:

<%= check_box_tag("Search Streams", "search_streams") %>

or
<%= check_box_tag("Search Streams", "event[search_streams]") %>

the second version would (if I got the syntax right) add the param to
params[:event] instead creating it's own.

the return value would be in params[:search_streams] and be "0" for
false or "1" for checked. (As a string, not number)

So you could do
if params[:search_streams] == "1" then
...
end

Should this still work if I want to be able to use the result of the
checkboxes from the Search view in my Event model? I have this now:

<% form_tag({:controller => 'search', :action => 'keyword'}, {:method =>
'post'}) do -%>
  <%= text_field_tag 'text' -%><%= submit_tag 'Search' -%> <br /> <br />

  <%= check_box_tag("Search Streams", "event[search_streams]") %>Search
Streams<br />
  <%= check_box_tag("Search Audiences", "event[search_auds]") %>Search
Audiences<br />

in my search index, and in my Event model I have:

def self.search_streams
  return params[:search_streams] == "1"
  end

  def self.search_auds
     return params[:search_auds] == "1"
  end

def self.search(keywords, options = {}, *args)
...
if search_streams
   for cal in streams
...

if search_auds
    for cal in audiences

It's telling me "undefined local variable or method `params' for
Event:Class"
with this, I probably just did something wrong with what you said.

Thanks so much for your help!

Should this still work if I want to be able to use the result of the
checkboxes from the Search view in my Event model? I have this now:

no, you can't access the params hash from within the model
(You could handle that by passing it to a method, but that's not the way
to go)

In case you want to access in in te model just do that:
Add an attr_accessor for those fields to your model like

attr_accessor :search_streams
attr_accessor :search_aud

Then you can continue with yor original code:

<%= f.check_box :search_streams, {:class=> "check"} %>

That way the fields can be handled like db columns in the model, without
having to actually have them in db

(Sorry, should have thought about that option)

Thorsten Mueller wrote:

attr_accessor :search_streams
attr_accessor :search_aud

Then you can continue with yor original code:

<%= f.check_box :search_streams, {:class=> "check"} %>

That way the fields can be handled like db columns in the model, without
having to actually have them in db

(Sorry, should have thought about that option)

okay so i put the attr_accessor statements in the Event model, and i
changed my boxes back how I had them, but the methods don't seem to be
getting a value because my searches give no results no matter what I
check:

methods in the Event model:

  def self.search_streams
  return :search_streams == 1
  end

  def self.search_auds
     return :search_auds == 1
  end