Unable to get simple ruby on rails Search to work :/

Posted by edu222 on Stack Overflow See other posts from Stack Overflow or by edu222
Published on 2010-05-03T02:01:23Z Indexed on 2010/05/03 2:08 UTC
Read the original article Hit count: 493

Filed under:
|

I am new to RoR, any help would be greatly appreciated :)
I have a basic scaffolding CRUD app to add customers.
I am trying to search by first_name or last_name fields.

The error that I am getting is:

NoMethodError in Clientes#find You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

Extracted source (around line #9):

6:     <th>Apellido</th>
7:     </tr>
8: 
9:   <% for cliente in @clientes %>
10:   <tr>
11:     <td><%=h cliente.client_name %></td>
12:     <td><%=h cliente.client_lastname %></td>

Application Trace

C:/Rails/clientes/app/views/clientes/find.html.erb:9:in `_run_erb_app47views47clientes47find46html46erb'

My find function in controllers/clientes_controlee.rb is:

# Find
def find
  @cliente = Cliente.find(:all, 
  :conditions=>["client_name = ? OR client_lastname = ?", params[:search_string], params[:search_string]])

end

My views/layouts clientes.html.erb form code fragment is:

<span style="text-align: right">
<% form_tag "/clientes/find" do %>
<%= text_field_tag :search_string %>
<%= submit_tag "Search" %>
<% end %>
</span>

The search template I created in views/clientes/find.html.erb:

<h1>Listing clientes for <%= params[:search_string] %></h1>

<table>
<tr>
<th>Nombre</th>
<th>Apellido</th>
</tr>
<% for cliente in @clientes %>
<tr>
<td><%=h cliente.client_name %></td>
<td><%=h cliente.client_lastname %></td>
<td><%= link_to 'Mostrar', cliente %></td>
<td><%= link_to 'Editar', edit_cliente_path(cliente) %></td>
<td><%= link_to 'Eliminar', cliente, :confirm =>'Estas Seguro de que desear eliminar a   este te cliente?', :method => :delete %></td>
</tr>
<% end %>
</table>


<%= link_to 'Atras', clientes_path %>

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about search