Forum: Ruby on Rails How to search in your application?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
26ca684555abbdb44f01bd1e165e649b?d=identicon&s=25 Vince W. (vince)
on 2006-04-26 20:52
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..
068f60670a8f31323854f945105702db?d=identicon&s=25 Dick Dishkuvek (dishkuvek)
on 2006-04-26 21:54
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.
26ca684555abbdb44f01bd1e165e649b?d=identicon&s=25 Vince W. (vince)
on 2006-04-27 02:25
>
> What pieces do you have and what are you missing?

So far I've got a view:

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

<%= start_form_tag({}, :method => :get) %>
<p id="instructions">
  Search <%= text_field_tag 'q', params[:query] %> <%= submit_tag 'Go'
%>
</p>
<%= 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..
4a021d3e4528fb8ab78ef7fc4b6822cc?d=identicon&s=25 Bao Lee (burninglegion)
on 2006-04-27 08:35
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]}%')

????
26ca684555abbdb44f01bd1e165e649b?d=identicon&s=25 Vince W. (vince)
on 2006-04-27 16:56
> 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 </h3>

<table>
        <div class="search">
                <%= form_tag :action =>'search', :controller => 'post'
%>
                <td width="50" <%= text_field 'product','@query' %>
                <td width="100"><%= submit_tag "Search" %>
                <%= end_form_tag %>
        </div>
</table>

<table>
 <tr>
<% for column in Product.content_columns %>
            <th><%= column.human_name %></th>
         <% end %>
             </tr>

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

So, how to pass the variable from the view to the controller?  Am I way
off?
This topic is locked and can not be replied to.