LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can you help me with search in Ruby on Rails? (https://www.linuxquestions.org/questions/programming-9/can-you-help-me-with-search-in-ruby-on-rails-439205/)

viniosity 04-26-2006 10:07 PM

Can you help me with search in Ruby on Rails?
 
I'm trying to create a way to search in my Ruby on Rails program. I am using the TextSearch plugin which seems to be working ok. The problem I am having is passing the info from the view to the controller.

My controller, again based on TextSearch, is simply:

Code:

def search
        @results = Product.search query
 end

My view is a bit more complicated:

Code:

<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 %>

Any help would be greatly appreciated. Search is supposed to be pretty easy but somehow I can't quite get it..

bigsmoke 01-05-2007 06:25 PM

In your controller, you call query, but query isn't defined anywhere. What you probably want is params['query'].

So, in your controller, first do

Code:

@query = params['query']
before you do

Code:

@results = Product.search @query # Note the @query instead of query, because I assigned params['query'] to @query instead of to query
Then, in your view, you can use:

Code:

<td width="50" <%= text_field_tag 'query', @query %>
<td width="100"><%= submit_tag "Search" %>

Note, that you quoted @query in your view, which means that you get the literal value '@query' instead of the value of the @query instance variable. Also you didn't assign anything to the @query instance variable in your controller. (The instance variables in your controller are inherited by the views.)

Lastly, there don't seem to be many Rails coders on this forum (I've only just registered myself and I'm not a habitual reader), so next time asking your question on the excellent "Ruby on Rails: Talk" Google group may give you an answer sooner.


All times are GMT -5. The time now is 06:35 PM.