开发者

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

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="}

NoM开发者_C百科ethodError (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)


@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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜