# ADWR Question: Page 171

**URL:** <https://www.ruby-forum.com/t/adwr-question-page-171/48712>\
**Category:** Rails\
**Created:** [November 23, 2005, 7:10pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712 "2005-11-23T19:10:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kscott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@kscott](https://www.ruby-forum.com/u/kscott)\
**Post date:** [November 23, 2005, 7:10pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712/1 "2005-11-23T19:10:11Z")

</div>

I am a complete newbie to Ruby and Rails. I purchased the Agile Dev with  
Rails book this weekend, and am making my way through it.

I’m in the testing chapter, and trying to complete the “exercise for the  
reader” by implementing search on the website.

According to my functional test (identical to the book), I have a  
working search controller. I also got a results.rhtml page written, and  
can load it up with information through the browser.

My problem is this. I am trying to create a search page that lets the  
user enter the terms to search for, then invoke the search action on the  
search controller and display what matches. I have an index.rhtml that  
has a simple form (one text field) and a submit button to the search  
action. This seems to work, and I wind up on the results page.

However, the value I type into the text field does not appear to be  
submitted through the form to the action. I always get all books  
displayed on the results page (I also seem to have the flash statement  
[Found 3 products] showing up on the index, when it is not expected).

If someone could point me in the direction of my error, I would  
appreciate it.

Thanks,  
Ken S.

Here is what I have written:

index.rhtml:  
\<% @page\_title = “Search PragProg Online Store” %\>

\<%= start\_form\_tag({:action =\> “search”}) %\>

Enter search terms:  
\<%= text\_field(nil, “query”) %\>  
  
\<%= submit\_tag(" Search ") %\>

\<%= end\_form\_tag %\>

results.rhtml:  
\<% @page\_title = “Search Results”%\>

 \<% for product in @products %\> 

 ![]()

```
    <h3><%=h product.title %></h3>
    <%= product.description %>
    <span class="catalogprice"><%= fmt_price(product.price) 

```

%\>  
\<%= link\_to ‘Add to Cart’,  
{:controller =\> ‘store’,  
:action =\> ‘add\_to\_cart’, :id =\> product },  
:class =\> ‘addtocart’ %\>

  

&nbsp;
  
\<% end %\>

\<%= link\_to “Show my cart”, {:controller =\> “store”, :action =\>  
“display\_cart”} %\>

search\_controller.rb  
class SearchController \< ApplicationController

layout “store”

def index  
end

def search  
@products = Product.search(params[:query])  
flash[:notice] = “Found #{@products.size} product(s).”  
render “search/results”  
end  
end

---

<div class="post-metadata">

**Author:** ![kscott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@kscott](https://www.ruby-forum.com/u/kscott)\
**Post date:** [November 23, 2005, 7:46pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712/2 "2005-11-23T19:46:38Z")

</div>

kscott wrote:

> ```
> @products = Product.search(params[:query])
> 
> ```

shouldn’t that be:

@products = Product.find(params[:query])

---

<div class="post-metadata">

**Author:** ![kscott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@kscott](https://www.ruby-forum.com/u/kscott)\
**Post date:** [November 23, 2005, 7:48pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712/3 "2005-11-23T19:48:10Z")

</div>

On 11/23/05, Ken S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def search  
> @products = Product.search(params[:query])  
> flash[:notice] = “Found #{@products.size} product(s).”  
> render “search/results”  
> end  
> end

Does the method Product.search exist?

---

<div class="post-metadata">

**Author:** ![kscott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@kscott](https://www.ruby-forum.com/u/kscott)\
**Post date:** [November 23, 2005, 8:36pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712/4 "2005-11-23T20:36:33Z")

</div>

On 11/23/05, Thomas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> kscott wrote:
> 
> > ```
> > @products = Product.search(params[:query])
> > 
> > ```
> 
> shouldn’t that be:
> 
> @products = Product.find(params[:query])

Unless the ActiveRecord.find method changed a lot recently, I think  
that method expects to be passed an ID for one of the records. (so  
you can’t type in a product name)

What you probably want is something like  
Product.find\_by\_name(params[:query]), but that only works if you have  
an exact match.

---

<div class="post-metadata">

**Author:** ![kscott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7993a0/32.png) [@kscott](https://www.ruby-forum.com/u/kscott)\
**Post date:** [November 23, 2005, 8:54pm UTC](https://www.ruby-forum.com/t/adwr-question-page-171/48712/5 "2005-11-23T20:54:35Z")

</div>

Got it figured out. Joe is correct, to use find, you have to give it an  
id.

But that at least got me an error that dumped the params object being  
passed in.

The working line is this:  
Product.search(params[:search][:query]

I set up the query field inside of a search object, but didn’t know how  
to reference it in the controller. I think I need to read the PickAxe  
book some more, and get used to referencing data structures in Ruby.

Thank you all for the hints that got me to the right place. I feel so  
accomplished, adding a new capability to the web app that I didn’t type  
out of the book. Maybe I can learn this stuff!

I still have the problem where the flash object is not being cleared out  
after it is displayed. What I mean, is that in my search/results view, I  
print the flash[:notice]. If I then go to the store index, I still have  
the same message at the top.

Any ideas?

joevandyk wrote:

> On 11/23/05, Thomas M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > kscott wrote:
> > 
> > > ```
> > > @products = Product.search(params[:query])
> > > 
> > > ```
> > 
> > shouldn’t that be:
> > 
> > @products = Product.find(params[:query])
> 
> Unless the ActiveRecord.find method changed a lot recently, I think  
> that method expects to be passed an ID for one of the records. (so  
> you can’t type in a product name)
> 
> What you probably want is something like  
> Product.find\_by\_name(params[:query]), but that only works if you have  
> an exact match.
