# How to search in your application?

**URL:** <https://www.ruby-forum.com/t/how-to-search-in-your-application/58878>\
**Category:** Rails\
**Created:** [April 26, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878 "2006-04-26T20:52:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878/1 "2006-04-26T20:52:36Z")

</div>

I’ve got my app running with a table for product set up like the one  
from the Agiles book and would like to create a page to search those  
products. Is there a tutorial somewhere I can reference? I’ve gone  
through the source code for typo and rails-weenie but can’t find all the  
pieces to make it work on my own app.

Else, any help would be greatly appreciated…

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878/2 "2006-04-26T21:54:07Z")

</div>

Vince W. wrote:

> I’ve got my app running with a table for product set up like the one  
> from the Agiles book and would like to create a page to search those  
> products. Is there a tutorial somewhere I can reference? I’ve gone  
> through the source code for typo and rails-weenie but can’t find all the  
> pieces to make it work on my own app.
> 
> Else, any help would be greatly appreciated…

What pieces do you have and what are you missing? As far as I know,  
there are no tutorials just on searching. There are some snippets  
around the wiki, here is one that looks pretty good:  
[http://wiki.rubyonrails.com/rails/pages/TextSearch](http://wiki.rubyonrails.com/rails/pages/TextSearch)

If you are using a table, like the one in the rails book, you could do  
something like:

@results = Product.find(:all, :condition =\> “name LIKE  
‘%#{params[:search]}%’”)

This is assuming that params[:search] is coming through from the page  
submission.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 27, 2006, 2:25am UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878/3 "2006-04-27T02:25:19Z")

</div>

> What pieces do you have and what are you missing?

So far I’ve got a view:

# \<%= params[:query].blank? ? 'Search' : "Searching for '#{h params[:query]}'" %\>

\<%= start\_form\_tag({}, :method =\> :get) %\>

Search \<%= text\_field\_tag 'q', params[:query] %\> \<%= submit\_tag 'Go' %\>

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

\<% end -%\>

and then I modified your line and put it in my controller:

def search  
@results = Product.find(:all,:conditions =\> "title LIKE  
‘%#{params[:query]}%’ ")  
end

But that gives me a very generic compile error: compile error  
script/…/config/…/app/views/post/search.rhtml:12: parse error,  
unexpected kEND, expecting $

Ug. I thought this part would be simple…

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 27, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878/4 "2006-04-27T16:56:42Z")

</div>

> is there a difference if you do a
> 
> @results = Product.find\_all(“title LIKE ?”, ‘%#{params[:query]}%’)
> 
> ???

I actually went through and tried the solution from the TextSearch RoR  
wiki and it works somewhat. If I put @results = Product.search = “19”  
it does find all products with 19 in it. The problem I’m having is  
making that “19” a variable instead.

Here is my controller:

def search  
@results = Product.search @query  
#warn @product.query  
end

And my view:

\<h3 Search Here 
 \<%= form\_tag :action =\>'search', :controller =\> 'post' %\> 

| | \<%= submit\_tag "Search" %\> \<%= end\_form\_tag %\> |

\<% for column in Product.content\_columns %\> \<% end %\> 

```
     <% for product in @results %>
              <tr>
  <% for column in Product.content_columns %>
       <td><%=h product.send(column.name) %></td>
            <% end %>
    </tr>
    <% end %>

```

| \<%= column.human\_name %\> |
| --- |

So, how to pass the variable from the view to the controller? Am I way  
off?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 27, 2006, 8:35am UTC](https://www.ruby-forum.com/t/how-to-search-in-your-application/58878/5 "2006-04-27T08:35:41Z")

</div>

Vince W. wrote:

> def search  
> @results = Product.find(:all,:conditions =\> "title LIKE  
> ‘%#{params[:query]}%’ ")  
> end
> 
> But that gives me a very generic compile error: compile error  
> script/…/config/…/app/views/post/search.rhtml:12: parse error,  
> unexpected kEND, expecting $
> 
> Ug. I thought this part would be simple…

is there a difference if you do a

@results = Product.find\_all(“title LIKE ?”, ‘%#{params[:query]}%’)

???
