Null values in check box's

0

In my application, it is necessary that if the user unchecks all check boxes informed to him, a Array nulo or [].

I'm using params[:parametro1][:parametro2_ids] ||= [] after initializing my Action UPDATE, but if I try to uncheck all, rails returns in the log:

NoMethodError (undefined method '[]' for nil:NilClass):. Under normal logic conditions, this should be functional. How can I implement a function if no value is selected or unselected?

My Log file can help:

Started GET "/responsabilities/1/nested_knowledges?responsability_id=1" for 127.0.0.1 at 2014-06-16 09:05:44 -0300
Processing by ResponsabilitiesController#nested_knowledges as HTML
  Parameters: {"responsability_id"=>"1", "id"=>"1"}
  [1m[36mSetting Load (1.0ms)[0m  [1mSELECT 'settings'.* FROM 'settings' WHERE 'settings'.'name' = 'session_lifetime' LIMIT 1[0m
  [1m[35mSetting Load (0.0ms)[0m  SELECT 'settings'.* FROM 'settings' WHERE 'settings'.'name' = 'session_timeout' LIMIT 1
  [1m[36m (0.0ms)[0m  [1mSELECT MAX('settings'.'updated_on') AS max_id FROM 'settings' [0m
Creating scope :sorted. Overwriting existing method User.sorted.
  [1m[35mUser Load (0.0ms)[0m  SELECT 'users'.* FROM 'users' WHERE 'users'.'type' IN ('User', 'AnonymousUser') AND 'users'.'status' = 1 AND 'users'.'id' = 1 LIMIT 1
  Current user: admin (id=1)
  [1m[36mResponsability Load (0.0ms)[0m  [1mSELECT 'responsabilities'.* FROM 'responsabilities' WHERE 'responsabilities'.'id' = 1 ORDER BY nome ASC LIMIT 1[0m
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in #<Module:0x49c03b0> instead. (called from realtime at c:/Ruby193/lib/ruby/1.9.1/benchmark.rb:295)
  Rendered plugins/uc_rh/app/views/uc_rh/_form.html.erb (3.0ms)
  [1m[35mKnowledge Load (0.0ms)[0m  SELECT 'knowledges'.* FROM 'knowledges' ORDER BY nome ASC
  [1m[36mKnowledge Exists (1.0ms)[0m  [1mSELECT 1 AS one FROM 'knowledges' INNER JOIN 'knowledges_responsabilities' ON 'knowledges'.'id' = 'knowledges_responsabilities'.'knowledge_id' WHERE 'knowledges_responsabilities'.'responsability_id' = 1 AND 'knowledges'.'id' = 1 ORDER BY nome ASC LIMIT 1[0m
  [1m[35mKnowledge Exists (1.0ms)[0m  SELECT 1 AS one FROM 'knowledges' INNER JOIN 'knowledges_responsabilities' ON 'knowledges'.'id' = 'knowledges_responsabilities'.'knowledge_id' WHERE 'knowledges_responsabilities'.'responsability_id' = 1 AND 'knowledges'.'id' = 3 ORDER BY nome ASC LIMIT 1
  [1m[36mKnowledge Exists (0.0ms)[0m  [1mSELECT 1 AS one FROM 'knowledges' INNER JOIN 'knowledges_responsabilities' ON 'knowledges'.'id' = 'knowledges_responsabilities'.'knowledge_id' WHERE 'knowledges_responsabilities'.'responsability_id' = 1 AND 'knowledges'.'id' = 2 ORDER BY nome ASC LIMIT 1[0m
  Rendered plugins/uc_rh/app/views/responsabilities/nested_knowledges.html.erb within layouts/base (529.0ms)
  [1m[35mSetting Load (0.0ms)[0m  SELECT 'settings'.* FROM 'settings' WHERE 'settings'.'name' = 'app_title' LIMIT 1
  [1m[36mSetting Load (0.0ms)[0m  [1mSELECT 'settings'.* FROM 'settings' WHERE 'settings'.'name' = 'ui_theme' LIMIT 1[0m
  [1m[35mUserPreference Load (0.0ms)[0m  SELECT 'user_preferences'.* FROM 'user_preferences' WHERE 'user_preferences'.'user_id' = 1 LIMIT 1
  [1m[36mSQL (8.0ms)[0m  [1mSELECT 'members'.'id' AS t0_r0, 'members'.'user_id' AS t0_r1, 'members'.'project_id' AS t0_r2, 'members'.'created_on' AS t0_r3, 'members'.'mail_notification' AS t0_r4, 'projects'.'id' AS t1_r0, 'projects'.'name' AS t1_r1, 'projects'.'description' AS t1_r2, 'projects'.'homepage' AS t1_r3, 'projects'.'is_public' AS t1_r4, 'projects'.'parent_id' AS t1_r5, 'projects'.'created_on' AS t1_r6, 'projects'.'updated_on' AS t1_r7, 'projects'.'identifier' AS t1_r8, 'projects'.'status' AS t1_r9, 'projects'.'lft' AS t1_r10, 'projects'.'rgt' AS t1_r11, 'projects'.'inherit_members' AS t1_r12, 'roles'.'id' AS t2_r0, 'roles'.'name' AS t2_r1, 'roles'.'position' AS t2_r2, 'roles'.'assignable' AS t2_r3, 'roles'.'builtin' AS t2_r4, 'roles'.'permissions' AS t2_r5, 'roles'.'issues_visibility' AS t2_r6 FROM 'members' LEFT OUTER JOIN 'projects' ON 'projects'.'id' = 'members'.'project_id' LEFT OUTER JOIN 'member_roles' ON 'member_roles'.'member_id' = 'members'.'id' LEFT OUTER JOIN 'roles' ON 'roles'.'id' = 'member_roles'.'role_id' WHERE 'members'.'user_id' = 1 AND (projects.status<>9) ORDER BY projects.name[0m
Completed 200 OK in 1218ms (Views: 836.0ms | ActiveRecord: 129.0ms)


Started PUT "/responsabilities/1" for 127.0.0.1 at 2014-06-16 09:05:48 -0300
Processing by ResponsabilitiesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Rp/n7f94mkgQKUkySaGqp+lhcKuMiv8WqPsIIlzkZ24=", "commit"=>"Enviar", "id"=>"1"}
  [1m[35m (0.0ms)[0m  SELECT MAX('settings'.'updated_on') AS max_id FROM 'settings' 
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT 'users'.* FROM 'users' WHERE 'users'.'type' IN ('User', 'AnonymousUser') AND 'users'.'status' = 1 AND 'users'.'id' = 1 LIMIT 1[0m
  Current user: admin (id=1)
Completed 500 Internal Server Error in 4ms

**NoMethodError (undefined method '[]' for nil:NilClass):
  plugins/uc_rh/app/controllers/responsabilities_controller.rb:59:in 'update'
  actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in 'send_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:167:in 'process_action'
  actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in 'process_action'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in 'block in process_action'**
  activesupport (3.2.13) lib/active_support/callbacks.rb:458:in '_run__296254661__process_action__268418134__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in '__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in '_run_process_action_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in 'run_callbacks'
  actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in 'process_action'
  actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in 'process_action'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in 'block in process_action'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in 'block in instrument'
  activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in 'instrument'
  activesupport (3.2.13) lib/active_support/notifications.rb:123:in 'instrument'
  actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in 'process_action'
  actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in 'process_action'
  activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in 'process_action'
  actionpack (3.2.13) lib/abstract_controller/base.rb:121:in 'process'
  actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in 'process'
  actionpack (3.2.13) lib/action_controller/metal.rb:203:in 'dispatch'
  actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in 'dispatch'
  actionpack (3.2.13) lib/action_controller/metal.rb:246:in 'block in action'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in 'call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in 'dispatch'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in 'call'
  journey (1.0.4) lib/journey/router.rb:68:in 'block in call'
  journey (1.0.4) lib/journey/router.rb:56:in 'each'
  journey (1.0.4) lib/journey/router.rb:56:in 'call'
  actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in 'call'
  rack-openid (1.4.1) lib/rack/openid.rb:99:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in 'call'
  rack (1.4.5) lib/rack/etag.rb:23:in 'call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in 'call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in 'context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in 'call'
  activerecord (3.2.13) lib/active_record/query_cache.rb:64:in 'call'
  activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:28:in 'block in call'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in '_run__878222876__call__309816011__callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:405:in '__run_callback'
  activesupport (3.2.13) lib/active_support/callbacks.rb:385:in '_run_call_callbacks'
  activesupport (3.2.13) lib/active_support/callbacks.rb:81:in 'run_callbacks'
  actionpack (3.2.13) lib/action_dispatch/middleware/callbacks.rb:27:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/reloader.rb:65:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in 'call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in 'call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in 'block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in 'tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in 'call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in 'call'
  rack (1.4.5) lib/rack/runtime.rb:17:in 'call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in 'call'
  rack (1.4.5) lib/rack/lock.rb:15:in 'call'
  actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in 'call'
  railties (3.2.13) lib/rails/engine.rb:479:in 'call'
  railties (3.2.13) lib/rails/application.rb:223:in 'call'
  rack (1.4.5) lib/rack/content_length.rb:14:in 'call'
  railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in 'call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in 'service'
  c:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:138:in 'service'
  c:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:94:in 'run'
  c:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in 'block in start_thread'
    
asked by anonymous 11.06.2014 / 15:30

1 answer

1

The issue has been resolved. Very simple thing:

In my form, I set up the scheme

<fieldset class="box">
    <legend>
        <%= l(:lbl_knowledge)%>
    </legend>
    <% for competence in Competence.find(:all) %>
    <%= check_box_tag "responsability[competence_ids][]", competence.id, @responsability.competences.include?(competence)  -%>
    <%= competence.nome %>
    <br/>
    <% end %>
</fieldset>
<div class="actions">
    <%= hidden_field_tag "responsability[competence_ids][]", nil %>
    <%= f.submit l(:send) %>
    <br/>
</div>
<br/>

And the line I needed to make Rails understand that if there were no values, put null values is as follows:

        <%= hidden_field_tag "responsability[competence_ids][]", nil %>

Problem solved. Thank you all.

@Andrey, @Guilherme Bernal.

    
16.06.2014 / 14:40