How to search in your application?


#1

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…


#2

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

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.


#3

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…


#4

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?


#5

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]}%’)

???