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.. -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
> > 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.. -- Posted via http://www.ruby-forum.com/.
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]}%'') ???? -- Posted via http://www.ruby-forum.com/.
> 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? -- Posted via http://www.ruby-forum.com/.