# BeginnerQ: Combining a form\_for with find() to search mysql

**URL:** <https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705>\
**Category:** Rails\
**Created:** [April 11, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705 "2007-04-11T19:29:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2007, 7:29pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/1 "2007-04-11T19:29:26Z")

</div>

I’ve looked over the AgileROR section on forms and the secion on  
find() but I am still a bit confused. What I want to do it pretty  
simple: I’ve got my inventory listing working and at the top I want  
the user to be able to narrow the listing based on a few columns in  
the database using a select form. I.E., I have a select pull-down  
that lists all of the makes in the :make column in the database (and  
doesn’t list the same make twice) and the user can select one of the  
makes and hit submit, then the user is given the inventory listing  
with just those makes. Most of the search related documentation seems  
to deal with fulltext search, which is not what I want. Just a select  
for :make, :model, :price and :carvantrucksuv – I know how to  
define :choices for :price and :carvantrucksuv in the model. But I  
can’t seem to get the form or the dynamic list for make and model to  
work. (I think I use :action =\> :create but I’m pretty lost)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/2 "2007-04-12T15:47:06Z")

</div>

> Just a select  
> for :make, :model, :price and :carvantrucksuv – I know how to  
> define :choices for :price and :carvantrucksuv in the model. But I  
> can’t seem to get the form or the dynamic list for make and model to  
> work. (I think I use :action =\> :create but I’m pretty lost)

I think you want the form.select method in your view.

> **[ActionView::Helpers::FormOptionsHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000506)**
>
> Provides a number of methods for turning different kinds of containers into a set of option tags.

Since you have the Agile web development book, I’ll refer you to page  
number 485, under “selection lists” (that’s in the 2nd edition of the  
book).

Best regards,

Chris

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/3 "2007-04-12T16:51:58Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’ve looked over the AgileROR section on forms and the secion on  
> find() but I am still a bit confused. What I want to do it pretty  
> simple: I’ve got my inventory listing working and at the top I want  
> the user to be able to narrow the listing based on a few columns in  
> the database using a select form. I.E., I have a select pull-down  
> that lists all of the makes in the :make column in the database (and  
> doesn’t list the same make twice) and the user can select one of the  
> makes and hit submit, then the user is given the inventory listing  
> with just those makes. Most of the search related documentation seems  
> to deal with fulltext search, which is not what I want. Just a select  
> for :make, :model, :price and :carvantrucksuv – I know how to  
> define :choices for :price and :carvantrucksuv in the model. But I  
> can’t seem to get the form or the dynamic list for make and model to  
> work. (I think I use :action =\> :create but I’m pretty lost)

On closer reading, I think you might want to do something like this :  
[How to paginate, sort and search a table with Ajax and Rails · dev.nozav.org](http://dev.nozav.org/rails_ajax_table.html)

That page is for an older version of rails I think, but the concepts are  
the same.

Chris

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/4 "2007-04-12T17:44:34Z")

</div>

I think that might work, I’m going to give it a try. I was going to  
try and do it without AJAX, but since it doesn’t seem to be spelled  
out anywhere else using just a simple form, AJAX is fine. Although  
I’ll have to figure out something with price constraints form.select.

On Apr 12, 10:51 am, Chris L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/5 "2007-04-12T18:52:46Z")

</div>

Bam! Got it! Just forgot a \<%= javascript\_include\_tag :defaults %\>.  
It works great. Results like this make me want to continue learning  
how to program. Now I’ve just got to figure out how to combine two or  
three of these to narrow the results.

On Apr 12, 11:43 am, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 9:03pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/6 "2007-04-12T21:03:00Z")

</div>

One hitch:

Enter the maximum price: _TEXTBOX_

Code:

conditions = [“price \< ?”, “%#{@params[:query]}%”] unless  
@params[:query].nil?

Doesn’t work, I’ve got the code wrong.

On Apr 12, 12:51 pm, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 12, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/beginnerq-combining-a-form-for-with-find-to-search-mysql/91705/7 "2007-04-12T18:28:39Z")

</div>

Crazy thing, I can search it now but AJAX doesn’t seem to be working:  
I have to enter the text in the box and hit enter. Ah!

On Apr 12, 11:43 am, “removed\_email\_address@domain.invalid”
