Forum: Ruby on Rails Problems when applying search to filter rows.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
MSC MSC (Guest)
on 2006-05-31 01:37
(Received via mailing list)
Hello,
I am trying to create in the main Layout of the application fields of
selection of fields of the model, and other to insert its value. Next I
put
a search button, with the idea to leak by the search in the view of the
model where it executes this button. I have generated the views with
ajaxscaffold.
I do not know if what I try to do it does of this form or another one.

My code is the following one:

- Main Layout  application.rhtml:

  <div id="msg_search" class='<%=@srch_class%>'>
  <%= form_tag :action => 'buscar' %>
  <p align="center">
    <%=_('Búsqueda en el campo:')%>&nbsp;
    <select name="search_field">
      <%= options_for_select(@campostabla, @params[:search_field]) %>
    </select>&nbsp;
    <label for="search_value"><%=_('igual a:')%></label>&nbsp;
    <input type="text" name="search_value" value="<%=@search_value%>"
size='16' id='search_value'/>&nbsp;
        <%= submit_tag(_('Buscar'), :name=>'op')%>&nbsp;
  <%= end_form_tag %>
     </p>
  </div>

- Code in the controller Empresas_Controller.rb:

  def list
     @campostabla = Empresa.column_names  #Para el campo búsqueda
  end

    # Para el botón buscar (Search Button)
  def buscar
    @search_field = @params['search_field']
    @search_value = @params['search_value']
    Empresa.find(:all, :conditions => ["? = ?", @search_field,
@search_value])
    render_action 'list'
  end

- When pressing the button search (Buscar), gives the following error
me:

NoMethodError in Empresas#buscar

Showing app/views/layouts/application.rhtml where line #266 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.inject

Extracted source (around line #266):

263:   <p align="center">
264:     <%=_('Búsqueda en el campo:')%>&nbsp;
265:     <select name="search_field">
266:       <%= options_for_select(@campostabla, @params[:search_field])
%>
267:     </select>&nbsp;
268:     <label for="search_value"><%=_('igual a:')%></label>&nbsp;
269:     <input type="text" name="search_value"
value="<%=@search_value%>"
size='16' id='search_value'/>&nbsp;

RAILS_ROOT: script/../config/..
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/form_options_helper.rb:114:in
`options_for_select'
#{RAILS_ROOT}/app/views/layouts/application.rhtml:266:in
`_run_rhtml_layouts_application'
lib/globalize/rails/action_view.rb:15:in `render_file'
#{RAILS_ROOT}/app/controllers/empresas_controller.rb:32:in `buscar'
#{RAILS_ROOT}/app/controllers/application.rb:29:in `process'
#{RAILS_ROOT}/app/controllers/application.rb:28:in `catch'
#{RAILS_ROOT}/app/controllers/application.rb:28:in `process'

/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/form_options_helper.rb:114:in
`options_for_select'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in
`compile_and_render_template'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in
`render_template'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in
`globalize_old_render_file'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:249:in
`render_with_a_layout'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:738:in
`render_with_layout'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:689:in
`render_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`perform_action_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`measure'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:172:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:161:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:161:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require__'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require__'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
script/server:3

/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/helpers/form_options_helper.rb:114:in
`options_for_select'
#{RAILS_ROOT}/app/views/layouts/application.rhtml:266:in
`_run_rhtml_layouts_application'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in
`compile_and_render_template'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in
`render_template'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in
`globalize_old_render_file'
lib/globalize/rails/action_view.rb:15:in `render_file'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:249:in
`render_with_a_layout'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:738:in
`render_with_layout'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:689:in
`render_action'
#{RAILS_ROOT}/app/controllers/empresas_controller.rb:32:in `buscar'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`perform_action_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`measure'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`send'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
#{RAILS_ROOT}/app/controllers/application.rb:29:in `process'
#{RAILS_ROOT}/app/controllers/application.rb:28:in `catch'
#{RAILS_ROOT}/app/controllers/application.rb:28:in `process'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:172:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:161:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:161:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require__'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require__'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
script/server:3
Request
Parameters: {"op"=>"Buscar", "search_field"=>"companyname",
"search_value"=>"nombre empresa a buscar"}
Show session dump
---
Response
Headers: {"Status"=>"200 OK", "cookie"=>[], "Cache-Control"=>"no-cache"}

Thanks for your aid.

Greetings,

Manuel Salguero
Seville, Spain
This topic is locked and can not be replied to.