Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - undefined method `[]' for nil:NilClass in application.html.erb

Yet another problem with my Rails 3 app. I am running Ruby 1.9.3p484 with Rails 3.2.16 on Apache 2.4.7 with Phusion_Passenger 4.0.37.

I added the jquery-datatables-rails gem and after adding *= require dataTables/jquery.dataTables to my application.css I started to have problems. The error message I get is:

undefined method `[]' for nil:NilClass
Showing /var/www-ruby/SteamBundleHelper/app/views/layouts/application.html.erb where line #5 raised:

The top of my application.html.erb file (line 5 is the stylesheet_link_tag one):

<!DOCTYPE html>
<html>
<head>
  <title>Steam Bundle Helper</title>
  <%= stylesheet_link_tag    "application", media: "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>

My Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.16'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'jquery-datatables-rails', '~> 3.3.0'

gem 'minitest'

gem 'nokogiri'

gem 'pry-rails', :group => :development
gem 'pry-debugger', :group => :development
gem 'pry-remote', :group => :development

The Gemfile.lock:

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (3.2.16)
      actionpack (= 3.2.16)
      mail (~> 2.5.4)
    actionpack (3.2.16)
      activemodel (= 3.2.16)
      activesupport (= 3.2.16)
      builder (>= 3.0.0)
      erubis (~> 2.7.0)
      journey (~> 1.0.4)
      rack (>= 1.4.5)
      rack-cache (~> 1.2)
      rack-test (~> 0.6.1)
      sprockets (>= 2.2)
    activemodel (3.2.16)
      activesupport (= 3.2.16)
      builder (>= 3.0.0)
    activerecord (3.2.16)
      activemodel (= 3.2.16)
      activesupport (= 3.2.16)
      arel (~> 3.0.2)
      tzinfo (~> 0.3.29)
    activeresource (3.2.16)
      activemodel (= 3.2.16)
      activesupport (= 3.2.16)
    activesupport (3.2.16)
      i18n (~> 0.6, >= 0.6.4)
      multi_json (~> 1.0)
    arel (3.0.3)
    builder (3.2.2)
    coderay (1.1.1)
    coffee-rails (3.2.2)
      coffee-script (>= 2.2.0)
      railties (~> 3.2.0)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.10.0)
    columnize (0.9.0)
    concurrent-ruby (1.0.1)
    debugger (1.6.8)
      columnize (>= 0.3.1)
      debugger-linecache (~> 1.2.0)
      debugger-ruby_core_source (~> 1.3.5)
    debugger-linecache (1.2.0)
    debugger-ruby_core_source (1.3.8)
    erubis (2.7.0)
    execjs (2.6.0)
    i18n (0.7.0)
    journey (1.0.4)
    jquery-datatables-rails (3.3.0)
      actionpack (>= 3.1)
      jquery-rails
      railties (>= 3.1)
      sass-rails
    jquery-rails (3.1.4)
      railties (>= 3.0, < 5.0)
      thor (>= 0.14, < 2.0)
    json (1.8.3)
    mail (2.5.4)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    method_source (0.8.2)
    mime-types (1.25.1)
    mini_portile2 (2.0.0)
    minitest (5.8.4)
    multi_json (1.11.2)
    nokogiri (1.6.7.2)
      mini_portile2 (~> 2.0.0.rc2)
    polyglot (0.3.5)
    pry (0.10.3)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    pry-debugger (0.2.3)
      debugger (~> 1.3)
      pry (>= 0.9.10, < 0.11.0)
    pry-rails (0.3.4)
      pry (>= 0.9.10)
    pry-remote (0.1.8)
      pry (~> 0.9)
      slop (~> 3.0)
    rack (1.6.4)
    rack-cache (1.6.1)
      rack (>= 0.4)
    rack-ssl (1.3.4)
      rack
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (3.2.16)
      actionmailer (= 3.2.16)
      actionpack (= 3.2.16)
      activerecord (= 3.2.16)
      activeresource (= 3.2.16)
      activesupport (= 3.2.16)
      bundler (~> 1.0)
      railties (= 3.2.16)
    railties (3.2.16)
      actionpack (= 3.2.16)
      activesupport (= 3.2.16)
      rack-ssl (~> 1.3.2)
      rake (>= 0.8.7)
      rdoc (~> 3.4)
      thor (>= 0.14.6, < 2.0)
    rake (10.5.0)
    rdoc (3.12.2)
      json (~> 1.4)
    sass (3.4.21)
    sass-rails (3.2.6)
      railties (~> 3.2.0)
      sass (>= 3.1.10)
      tilt (~> 1.3)
    slop (3.6.0)
    sprockets (3.5.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sqlite3 (1.3.11)
    thor (0.19.1)
    tilt (1.4.1)
    treetop (1.4.15)
      polyglot
      polyglot (>= 0.3.1)
    tzinfo (0.3.46)
    uglifier (2.7.2)
      execjs (>= 0.3.0)
      json (>= 1.8.0)

PLATFORMS
  ruby

DEPENDENCIES
  coffee-rails (~> 3.2.1)
  jquery-datatables-rails (~> 3.3.0)
  jquery-rails
  minitest
  nokogiri
  pry-debugger
  pry-rails
  pry-remote
  rails (= 3.2.16)
  sass-rails (~> 3.2.3)
  sqlite3
  uglifier (>= 1.0.3)

My application.css file:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require dataTables/jquery.dataTables
 */

body {
    font-size: 14px;
    font-family: Verdana, Tahoma, sans-serif;
}

.main_container {
    width: 1200px;
    margin: 0 auto;
}

.header {
    padding-bottom: 3em;
}

.profile_details {
    float: right;

}

As soon as I remove the *= require dataTables/jquery.dataTables from application.css the error goes away. The problem is that I need the Datatables CSS files. I fiddled with a lot of things but ended up reverting them since they did not solve my issue. Any idea is welcome.

EDIT: as I'm not too familiar with either sprockets or sass, what would be the best way to remove sprockets, which seems to be the culprit, and to compile all .scss files directly? Should I @import them all into application.css?

EDIT #2 (2016-03-29): as requested in the comments I'm appending the (very long) full trace:

sass-rails (3.2.6) lib/sass/rails/template_handlers.rb:81:in `sass_options'
sass-rails (3.2.6) lib/sass/rails/template_handlers.rb:106:in `evaluate'
/usr/lib/ruby/vendor_ruby/tilt/template.rb:103:in `render'
sprockets (3.5.2) lib/sprockets/legacy_tilt_processor.rb:25:in `call'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.5.2) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.5.2) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.5.2) lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'
sprockets (3.5.2) lib/sprockets/loader.rb:44:in `load'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `yield'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `default'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.5.2) lib/sprockets/bundle.rb:23:in `block in call'
sprockets (3.5.2) lib/sprockets/utils.rb:183:in `dfs'
sprockets (3.5.2) lib/sprockets/bundle.rb:24:in `call'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.5.2) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.5.2) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.5.2) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.5.2) lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'
sprockets (3.5.2) lib/sprockets/loader.rb:44:in `load'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `yield'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `default'
sprockets (3.5.2) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.5.2) lib/sprockets/base.rb:66:in `find_asset'
sprockets (3.5.2) lib/sprockets/environment.rb:30:in `find_asset'
sprockets (3.5.2) lib/sprockets/base.rb:92:in `[]'
/usr/lib/ruby/vendor_ruby/sprockets/helpers/rails_helper.rb:126:in `asset_for'
/usr/lib/ruby/vendor_ruby/sprockets/helpers/rails_helper.rb:44:in `block in stylesheet_link_tag'
/usr/lib/ruby/vendor_ruby/sprockets/helpers/rails_helper.rb:43:in `collect'
/usr/lib/ruby/vendor_ruby/sprockets/helpers/rails_helper.rb:43:in `stylesheet_link_tag'
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb___3648343695788242355_69996357879600'
/usr/lib/ruby/vendor_ruby/action_view/template.rb:145:in `block in render'
/usr/lib/ruby/vendor_ruby/active_support/notifications.rb:125:in `instrument'
/usr/lib/ruby/vendor_ruby/action_view/template.rb:143:in `render'
/usr/lib/ruby/vendor_ruby/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
/usr/lib/ruby/vendor_ruby/action_view/renderer/template_renderer.rb:45:in `render_template'
/usr/lib/ruby/vendor_ruby/action_view/renderer/template_renderer.rb:18:in `render'
/usr/lib/ruby/vendor_ruby/action_view/renderer/renderer.rb:36:in `render_template'
/usr/lib/ruby/vendor_ruby/action_view/renderer/renderer.rb:17:in `render'
/usr/lib/ruby/vendor_ruby/abstract_controller/rendering.rb:110:in `_render_template'
/usr/lib/ruby/vendor_ruby/action_controller/metal/streaming.rb:225:in `_render_template'
/usr/lib/ruby/vendor_ruby/abstract_controller/rendering.rb:103:in `render_to_body'
/usr/lib/ruby/vendor_ruby/action_controller/metal/renderers.rb:28:in `render_to_body'
/usr/lib/ruby/vendor_ruby/action_controller/metal/compatibility.rb:50:in `render_to_body'
/usr/lib/ruby/vendor_ruby/abstract_controller/rendering.rb:88:in `render'
/usr/lib/ruby/vendor_ruby/action_controller/metal/rendering.rb:16:in `render'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
/usr/lib/ruby/vendor_ruby/active_support/core_ext/benchmark.rb:5:in `block in ms'
/usr/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
/usr/lib/ruby/vendor_ruby/active_support/core_ext/benchmark.rb:5:in `ms'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:40:in `block in render'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
/usr/lib/ruby/vendor_ruby/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:39:in `render'
/usr/lib/ruby/vendor_ruby/action_controller/metal/implicit_render.rb:10:in `default_render'
/usr/lib/ruby/vendor_ruby/action_controller/metal/implicit_render.rb:5:in `send_action'
/usr/lib/ruby/vendor_ruby/abstract_controller/base.rb:167:in `process_action'
/usr/lib/ruby/vendor_ruby/action_controller/metal/rendering.rb:10:in `process_action'
/usr/lib/ruby/vendor_ruby/abstract_controller/callbacks.rb:18:in `block in process_action'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:414:in `_run__3287941951740287054__process_action__2444081902642250416__callbacks'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:405:in `__run_callback'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:81:in `run_callbacks'
/usr/lib/ruby/vendor_ruby/abstract_controller/callbacks.rb:17:in `process_action'
/usr/lib/ruby/vendor_ruby/action_controller/metal/rescue.rb:29:in `process_action'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/usr/lib/ruby/vendor_ruby/active_support/notifications.rb:123:in `block in instrument'
/usr/lib/ruby/vendor_ruby/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/lib/ruby/vendor_ruby/active_support/notifications.rb:123:in `instrument'
/usr/lib/ruby/vendor_ruby/action_controller/metal/instrumentation.rb:29:in `process_action'
/usr/lib/ruby/vendor_ruby/action_controller/metal/params_wrapper.rb:207:in `process_action'
/usr/lib/ruby/vendor_ruby/active_record/railties/controller_runtime.rb:18:in `process_action'
/usr/lib/ruby/vendor_ruby/abstract_controller/base.rb:121:in `process'
/usr/lib/ruby/vendor_ruby/abstract_controller/rendering.rb:45:in `process'
/usr/lib/ruby/vendor_ruby/action_controller/metal.rb:203:in `dispatch'
/usr/lib/ruby/vendor_ruby/action_controller/metal/rack_delegation.rb:14:in `dispatch'
/usr/lib/ruby/vendor_ruby/action_controller/metal.rb:246:in `block in action'
/usr/lib/ruby/vendor_ruby/action_dispatch/routing/route_set.rb:73:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/routing/route_set.rb:73:in `dispatch'
/usr/lib/ruby/vendor_ruby/action_dispatch/routing/route_set.rb:36:in `call'
/usr/lib/ruby/vendor_ruby/journey/router.rb:68:in `block in call'
/usr/lib/ruby/vendor_ruby/journey/router.rb:56:in `each'
/usr/lib/ruby/vendor_ruby/journey/router.rb:56:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/routing/route_set.rb:608:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/head.rb:14:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/params_parser.rb:21:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/cookies.rb:341:in `call'
/usr/lib/ruby/vendor_ruby/active_record/query_cache.rb:64:in `call'
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:405:in `_run__411984161693097463__call__4601814333716727330__callbacks'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:405:in `__run_callback'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:385:in `_run_call_callbacks'
/usr/lib/ruby/vendor_ruby/active_support/callbacks.rb:81:in `run_callbacks'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/callbacks.rb:27:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/reloader.rb:65:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/remote_ip.rb:31:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/show_exceptions.rb:56:in `call'
/usr/lib/ruby/vendor_ruby/rails/rack/logger.rb:32:in `call_app'
/usr/lib/ruby/vendor_ruby/rails/rack/logger.rb:16:in `block in call'
/usr/lib/ruby/vendor_ruby/active_support/tagged_logging.rb:22:in `tagged'
/usr/lib/ruby/vendor_ruby/rails/rack/logger.rb:16:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
/usr/lib/ruby/vendor_ruby/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
/usr/lib/ruby/vendor_ruby/action_dispatch/middleware/static.rb:63:in `call'
/usr/lib/ruby/vendor_ruby/rails/engine.rb:484:in `call'
/usr/lib/ruby/vendor_ruby/rails/application.rb:231:in `call'
/usr/lib/ruby/vendor_ruby/rails/railtie/configurable.rb:30:in `method_missing'
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:77:in `process_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:142:in `accept_and_process_next_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'

EDIT #3 (2016-03-30):

If I remove all the require lines from my application.css I get no error. So the error is coming from any require directive.

like image 802
Technoh Avatar asked Nov 27 '25 15:11

Technoh


2 Answers

It looks like you're not loading your asset pipeline, specifically the stylesheets and javascripts. After gem installation, run rails generate jquery:datatables:install from the command line. Your application.js and application.css files should look like this:

application.js

...
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require turbolinks
//= require_tree .

application.css

...
*= require_tree .
*= require dataTables/jquery.dataTables
*= require_self
*/
like image 194
Dan Crews Avatar answered Nov 30 '25 06:11

Dan Crews


I'm pretty sure that this doesn't have anything to do with sprockets or the gem itself. I had a problem like this a while back and the problem was with my code. Without any more information I can't really tell, check to make sure that your making the proper conditionals. Check out this other stack overflow response that helped me when I ran into this error. If you get the chance can you also post your controller method for this view page.

like image 21
Oscar Vazquez Avatar answered Nov 30 '25 06:11

Oscar Vazquez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!