Topic: Is there any known way to use CHECKBOX's to filter search results from

I need to use a set of CHECKBOX's to filter in/out results of a search from a list of tables

My thinking would be this check box holds a hash and the search by default searches all or none of these hashs hash being the table:field name and each check box click sends the on of off signal to the search hash

Am I kinda missing something ??

Re: Is there any known way to use CHECKBOX's to filter search results from

Sorry man, I'm not following what you're trying to do.

Re: Is there any known way to use CHECKBOX's to filter search results from

Basicly advanced search. kinda like
http://www.google.com/advanced_search?q … amp;rls=en

But with check boxes ; I'll post an image one sec

Re: Is there any known way to use CHECKBOX's to filter search results from

http://img130.imageshack.us/img130/7478/filterssr8.jpg

The search box is default like a search box. The filters will all be on by default, and clicking one of them off will remove that tables results from the search results

Also http://www.treeview.net/treemenu/3fr_checkbox.html

Last edited by tripdragon (2006-07-27 13:43:02)

Re: Is there any known way to use CHECKBOX's to filter search results from

That seems doable. On the controller side you can just toggle which columns are searched depending upon which checkbox was checked. Are you having a specific problem trying to implement this?

Railscasts - Free Ruby on Rails Screencasts

Re: Is there any known way to use CHECKBOX's to filter search results from

well I dont have any hard code yet.. I was just searching around to find some basic examples to read from for textboxes or custom buttons...

I did get this to work as the starting search just need to implement filters now..
http://wiki.rubyonrails.com/rails/pages/SearchGenerator

I am not trying to get anyone to do teh work for me,, I am just trying to learn from something more.. I just finished the entire of Ruby for rails and have a far greater understanding of whats going on,, And have learned scaffolds are not really of any use after you end up having to hack em all apart and such... So it's still a learning proccess for me ..

Re: Is there any known way to use CHECKBOX's to filter search results from

Ok so I have ferret working,, now I just need to figure out the filter portion..

Any clues where I should start ?

Re: Is there any known way to use CHECKBOX's to filter search results from

Here's a little code that could get you started... I'm watching several form fields using observe_form, but if it's only one, then use observe_field.

<!-- view.rhtml -->
    <%= start_form_tag({}, {:id => 'filters'}) %>
    <p>show only people marked for followup <%= check_box_tag('followup') %></p>
    <% p = Project.new %>
    <p>projects : <%= select_tag(:project, options_for_select(p.project_map.unshift(['-- make a selection --', '-- make a selection --']))) %></p>
    <p>search: <%= text_field_tag('search') %></p>
    <p><%= link_to_function('reset', 'resetFields()') %></p>
    <%= observe_form(:filters, :url => {:action => 'search'}, :frequency => 0.25, :update => 'results') %>
    <%= end_form_tag %>

# controller.rb

  def search
    @members = Member.find_filtered(:search => params[:search], :followup => params[:followup], :project => params[:project])   
    render :partial => 'list_results_table'
  end


The presumption this code makes is that you have built enough smarts into your model to perform this search.

Last edited by cwd (2006-07-30 02:45:58)

Re: Is there any known way to use CHECKBOX's to filter search results from

Hi there.

I've had similar issues to this, and I've tended to use what I call a condition builder.

So, assuming you've got:

* params[:search] contains the string the user is searching for

* params[:city], params[:state] and params[:county] will contain something if the checkbox is checked, and nothing if they're not. (Is that right? I haven't had a coffee yet, and haven't checked this! :-D)

* city, state and county are fields in your database that you wish to search

Then you could build a condition array, and use it, like this:

condition_builder = [""]

["city","state","county"].each do |check|
  if params[check.to_sym]
    # Append an "or" on run 2+
    condition_builder[0] << " or " if !condition_builder[0].empty?

    # Append what we want to search to the query string
    condition_builder[0] << check + " = ?"

    # Append the search criteria to the condition array
    condition_builder << params[:search]
  end
end

@foo = Items.find(:all, :condition => condition_builder)


There's not much error checking going on there - you'd want to ensure they've actually ticked some tick boxes for a start - possibly by checking !condition_builder[0].empty? - and give them a nice error.

I run into this problem quite often - I regularly want drop down lists that filter search results (e.g. "Show All Clients", "Show Live Clients", "Show Prospective Clients" etc.) - which is a simliar (if not identical) case - so if anyone has a better solution than mine, I'd like to hear about it.

Cheers,
NeilS.

Re: Is there any known way to use CHECKBOX's to filter search results from

You could do something like this if you move it to the model:

def self.find_filtered(keyword, options = {})
  conditions = []
  column_names.each do |column|
    conditions << "#{column} = #{sanitize(keyword)}" unless options[column].nil?
  end
  find(:all, :conditions => conditions.join(' or '))
end

Last edited by ryanb (2006-08-09 16:05:58)

Railscasts - Free Ruby on Rails Screencasts

Re: Is there any known way to use CHECKBOX's to filter search results from

Woa ! Lots of reading to do...
Hmm so the model is used after all...
Jezzes at what point of reading and over reading do you people get to the point that you can create this stuff !?! In all of the books I read I have found zero info for multi filter searching... I have also found nothing to point me to the list of params that are available like :conditions :all

Ok enough of that.. I thank you all for your great help.. I will try and get this intergrated into a very basic test scaffold and see if it works.. After which I will release it to everyone,, cause I feel sorry for the next chap that has to try this...

Re: Is there any known way to use CHECKBOX's to filter search results from

The Rails API is really useful for finding out the methods and parameters. Takes a while to get used to using it though.

Railscasts - Free Ruby on Rails Screencasts

Re: Is there any known way to use CHECKBOX's to filter search results from

ryanb wrote:

The Rails API is really useful for finding out the methods and parameters. Takes a while to get used to using it though.

Hmm I have been trying to read it but without a search tool to digg through it, sometimes it feels impossible..
Is there a pdf version of it anywhere ?>??, with pdf I can search really fast on macs..

14

Re: Is there any known way to use CHECKBOX's to filter search results from

No search function. Try::

http://railsmanual.org

Last edited by cwd (2006-08-04 12:36:58)

Re: Is there any known way to use CHECKBOX's to filter search results from

cwd wrote:

No search function. Try::

http://railsmanual.org

Cool!

Re: Is there any known way to use CHECKBOX's to filter search results from

I just use the in-browser search (command-F on a Mac). It's very basic, but it generally works for me.

Railscasts - Free Ruby on Rails Screencasts

Re: Is there any known way to use CHECKBOX's to filter search results from

I recommend checking out Krugle.  It's a search engine for source code.

18

Re: Is there any known way to use CHECKBOX's to filter search results from

Great tip. This looks really cool. Also check out:

http://gotapi.com/

Re: Is there any known way to use CHECKBOX's to filter search results from

Ok so I am now going through it all after a few days of studing..

<p>projects : <%= select_tag(:project, options_for_select(p.monkey_map.unshift(['-- make a selection --', '-- make a selection --']))) %></p>

this part monkey_map... I belive it to be a method.. what type of data would I put in the method to make it think.. I thought the model handeled a lot of the database pushing like the scaffold creates
  <% for column in Monkey.content_columns %>

hmm still reading

Re: Is there any known way to use CHECKBOX's to filter search results from

Okey dokey... I Am ALMOST THERE ! Once I started to learn the ERB is just ruby in blocks.. err values printed on screen it starts to make some sense..

I still don't see a logical way to get a check box to control the condition query strings like a hash lookup value but here is my code granted mostly just a hack of a tutorial for ajax search ... But once I know how to manipulate checkboxes to send the valuse to areas other than the database it will make sence to me

class ItemController < ApplicationController
#Our controller only defines one action, called list. This action will have to handle every request sent to our application.
  def list

    items_per_page = 10
#The reverse string in the sort parameter indicates that sorting should be made in descending order.
    sort = case params['sort']
           when "Keywords"  then "Keywords"
           when "Synopsis_Content"   then "Synopsis_Content"
           when "Bar_Code" then "Bar_Code"
           when "name_reverse"  then "name DESC"
           when "Time_Code_reverse"   then "Time_Code DESC"
           when "Bar_Code_reverse" then "Bar_Code DESC"
           end
#Then, a conditions variable is constructed if a query request parameter is present. It is an SQL-like instruction which will be used to filter our database query results based on the content of the name field.
    #conditions = ["name LIKE ?", "%#{params[:query]}%"] unless params[:query].nil?
  conditions = ["Keywords LIKE ? or Bar_Code LIKE ? or Synopsis_Content LIKE ?"] + ["%#{params[:query]}%"] * 3
#After that, we assign the total number of items in our database matching the conditions to the @total variable.
    @total = Item.count(:conditions => conditions)
    @items_pages, @items = paginate :items, :order => sort, :conditions => conditions, :per_page => items_per_page

    if request.xml_http_request?
      render :partial => "items_list", :layout => false
    end

  end

#def gooddog
  #@checkgooddog = gooddog.new
  #if @checkgooddog == (:action => "searchthis")
  #end

#end

end