这是一个哈希:
@rolesHash = {:update => {:link => 1, :task => 36}, :delete => {:link => 2, :job => 14}}
尝试访问时,我会遇到以下奇怪的特征:
@rolesHash[:update].present? # true
@rolesHash[:update].class # Hash
@rolesHash[:update].keys # undefined method nil for nil:NilClass
@rolesHash[:update][:task] # expecting '36' but get undefined method nil for nil:NilClass
为什么我无法正确访问此哈希?
14: @recruiters = @company.recruiters
15: render layout: 'notice'
<% @recruiters.each do |recruiter| %>
<% @user = User.find_by_roleable_type_and_roleable_id("Recruiter", recruiter.id) %>
<% @rolesHash = {} %>
<% roles = @user.roles.where("user_id = ? AND company_id = ?", @user.id, @company.id) %>
<% roles.each{|r| @rolesHash[r.role.to_sym] ||= {}; @rolesHash[r.role.to_sym][r.roleable_type.to_sym] = r.roleable_id}
...
<%= @rolesHash[:delete][:job] # error
...
<% end %>
undefined method `[]' for nil:NilClass
提取的来源(第148行):
147:
148: <%= @rolesHash[:delete][:job] %>
149:
Rails.root: /Users/ss/Documents/ss/app
app/views/recruiters/index.html.erb:148:in `block in _app_views_recruiters_index_html_erb__298609598644026366_70131760233080'
activerecord (3.2.13) lib/active_record/relation/delegation.rb:6:in `each'
activerecord (3.2.13) lib/active_record/relation/delegation.rb:6:in `each'
app/views/recruiters/index.html.erb:134:in `_app_views_recruiters_index_html_erb__298609598644026366_70131760233080'
actionpack (3.2.13) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.13) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.13) lib/action_view/template.rb:143:in `render'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.13) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
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_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/template_renderer.rb:18:in `render'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:154:in `block in render_with_newrelic'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/method_tracer.rb:234:in `trace_execution_scoped'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:153:in `render_with_newrelic'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.13) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.13) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.13) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/Users/ss/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (3.2.13) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:39:in `render'
app/controllers/recruiters_controller.rb:15:in `index'
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:480:in `_run__4384579426478621208__process_action__277342749717267414__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'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:32:in `block in process_action'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:365:in `perform_action_with_newrelic_trace'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:31: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'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
/Users/ss/.rvm/gems/ruby-2.0.0-p481/bundler/gems/rails-dev-boost-bf49caca6670/lib/rails_development_boost/async.rb:12:in `block in call'
/Users/ss/.rvm/gems/ruby-2.0.0-p481/bundler/gems/rails-dev-boost-bf49caca6670/lib/rails_development_boost/async.rb:39:in `block in synchronize'
/Users/ss/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/Users/ss/.rvm/gems/ruby-2.0.0-p481/bundler/gems/rails-dev-boost-bf49caca6670/lib/rails_development_boost/async.rb:39:in `synchronize'
/Users/ss/.rvm/gems/ruby-2.0.0-p481/bundler/gems/rails-dev-boost-bf49caca6670/lib/rails_development_boost/async.rb:12:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/rack/error_collector.rb:50:in `traced_call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/head.rb:14:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/flash.rb:242:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/cookies.rb:341:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activerecord (3.2.13) lib/active_record/query_cache.rb:64:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activerecord (3.2.13) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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__3461156388977909965__call__2048692231345738343__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'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rails-dev-tweaks (1.2.0) lib/rails_dev_tweaks/granular_autoload/middleware.rb:36:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
quiet_assets (1.0.3) lib/quiet_assets.rb:23:in `call_with_quiet_assets'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack-cache (1.2) lib/rack/cache/context.rb:136:in `forward'
rack-cache (1.2) lib/rack/cache/context.rb:245:in `fetch'
rack-cache (1.2) lib/rack/cache/context.rb:185:in `lookup'
rack-cache (1.2) lib/rack/cache/context.rb:66:in `call!'
rack-cache (1.2) lib/rack/cache/context.rb:51:in `call'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
railties (3.2.13) lib/rails/railtie/configurable.rb:30:in `method_missing'
newrelic_rpm (3.9.4.245) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
thin (1.6.2) lib/thin/connection.rb:86:in `block in pre_process'
thin (1.6.2) lib/thin/connection.rb:84:in `catch'
thin (1.6.2) lib/thin/connection.rb:84:in `pre_process'
thin (1.6.2) lib/thin/connection.rb:53:in `process'
thin (1.6.2) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.3) lib/eventmachine.rb:187:in `run'
thin (1.6.2) lib/thin/backends/base.rb:73:in `start'
thin (1.6.2) lib/thin/server.rb:162:in `start'
rack (1.4.5) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.13) lib/rails/commands/server.rb:70:in `start'
railties (3.2.13) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.13) lib/rails/commands.rb:50:in `tap'
railties (3.2.13) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:7:in `require'
script/rails:7:in `<main>'
答案 0 :(得分:0)
我猜您对@user.roles.where("user_id = ? AND company_id = ?", @user.id, @company.id)
的调用不会返回任何行。这会导致roles
为空,这反过来意味着roles.each
不会迭代任何内容,并且永远不会调用该块。
因此,当你到达 p>
<%= @rolesHash[:delete][:job]
@rolesHash
仍为{}
,并且该行将因您已粘贴的错误而失败。
为了验证这一点,我将首先在输出中添加两个调试行。在迭代之前就有一个:
<% Rails.logger.debug roles.inspect %>
<% roles.each{|r| @rolesHash[r.role.to_sym] ||= {}; @rolesHash[r.role.to_sym][r.roleable_type.to_sym] = r.roleable_id}
和访问@rolesHash之前的一个:
<% Rails.logger.debug @rolesHash.inspect %>
<%= @rolesHash[:delete][:job] %>
然后,查看您的日志文件应该可以让您查看实际数据是否与您希望出现的数据相匹配。