views:

157

answers:

1

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>
<br />
<%= link_to 'Atras', clientes_path %>

Dev Log

Processing ClientesController#index (for 127.0.0.1 at 2010-05-02 22:14:29) [GET] [4;36;1mCliente Load (1.0ms)[0m [0;1mSELECT * FROM "clientes" [0m Rendering template within layouts/clientes Rendering clientes/index Completed in 28ms (View: 19, DB: 1) | 200 OK [http://localhost/clientes]

Processing ClientesController#src (for 127.0.0.1 at 2010-05-02 22:14:36) [POST] Parameters: {"search_string"=>"eduardo calvachi", "commit"=>"Search", "authenticity_token"=>"mSaFeUAWdIWBNPkTufX2hdx7NaMaGfLSp1h78nTB7Ns="}

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/controllers/clientes_controller.rb:88:in `src'

Rendered rescues/_trace (48.0ms) Rendered rescues/_request_and_response (0.0ms) Rendering rescues/layout (internal_server_error)

Processing ClientesController#src (for 127.0.0.1 at 2010-05-02 22:20:28) [POST] Parameters: {"search_string"=>"eduardo calvachi", "commit"=>"Search", "authenticity_token"=>"mSaFeUAWdIWBNPkTufX2hdx7NaMaGfLSp1h78nTB7Ns="}

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/controllers/clientes_controller.rb:88:in `src'

Rendered rescues/_trace (92.0ms) Rendered rescues/_request_and_response (1.0ms) Rendering rescues/layout (internal_server_error)

Processing ClientesController#src (for 127.0.0.1 at 2010-05-02 22:36:12) [POST] Parameters: {"search_string"=>"eduardo calvachi", "commit"=>"Search", "authenticity_token"=>"mSaFeUAWdIWBNPkTufX2hdx7NaMaGfLSp1h78nTB7Ns="}

NoMethodError (You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[]): app/controllers/clientes_controller.rb:88:in `src'

Rendered rescues/_trace (39.0ms) Rendered rescues/_request_and_response (0.0ms) Rendering rescues/layout (internal_server_error)

A: 

@clientes is obviously the nil element.

Try putting your code into the index because you might have it as a restful resource or maybe find is a reserved name.

change your definition in the controller to the following:

def index
  params = params[:search_string]
  @clientes = Cliente.find(:all, :conditions=>["client_name = ? OR client_lastname = ?",params, params)
end

BTW - make sure you then have your old find view in your index view.

Sam
Sam, thanks for the suggestion, I replaced my find def with your index def, then i changed <% form_tag "/clientes/find" do %> to <% form_tag "/clientes/index" do %> but I now I am not even able to run /clientes. I get a noMethod error C:/Rails/clientes/app/controllers/clientes_controller.rb:88:in `index'what am I missing here?
edu222
Also I do want to be able to show the regular index of clientes in my app as well as the search results when needed. Wouldn't changing my old index view mess my old default index of clients up?
edu222
If I try something else and call my def src instead of find, use <% form_tag "/clientes/src" do %> in views/layout file and still use the code that you suggested, i get a different nil object NoMethodError: C:/Rails/clientes/app/controllers/clientes_controller.rb:88:in `src' RequestParameters:{"search_string"=>"eduardo calvachi", "commit"=>"Search", "authenticity_token"=>"mSaFeUAWdIWBNPkTufX2hdx7NaMaGfLSp1h78nTB7Ns="}
edu222
I think most people tag search on index because it generally does exactly the same thing that index does but with given params instead of listing all
Sam
check you development log and see which object is `nil` if you don't know just post the last 20 or so lines in your question.
Sam
I see, interesting input on the index-search sittuation.
edu222
Here are the las 20 lines of so:) || I actually added that portion of the log to the actual question.
edu222