Rails 3 - 未定义的方法`[]'for nil:application.html.erb中的NilClass

时间:2016-03-03 19:10:49

标签: jquery ruby-on-rails ruby ruby-on-rails-3 datatables

我的Rails 3应用程序的另一个问题。我使用Phusion_Passenger 4.0.37在Apache 2.4.7上运行带有Rails 3.2.16的Ruby 1.9.3p484。

我添加了jquery-datatables-rails gem并在将*= require dataTables/jquery.dataTables添加到application.css后,我开始遇到问题。我得到的错误信息是:

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

我的application.html.erb文件的顶部(第5行是stylesheet_link_tag文件):

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

我的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

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)

我的application.css文件:

/*
 * 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;

}

一旦我从*= require dataTables/jquery.dataTables移除application.css,错误就会消失。问题是我需要Datatables CSS文件。我摆弄了很多东西,但由于没有解决我的问题而最终还原了它们。任何想法都是受欢迎的。

编辑:因为我对sprocketssass不太熟悉,删除链轮的最佳方式是什么,这似乎是罪魁祸首,并直接编译所有.scss文件?我应该@import将它们全部变成application.css吗?

编辑#2(2016-03-29):根据评论中的要求,我附加了(很长)完整的跟踪:

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'

编辑#3(2016-03-30)

如果我从require删除所有application.css行,则不会出现错误。所以错误来自任何 require指令。

5 个答案:

答案 0 :(得分:1)

看起来您没有加载资产管道,特别是样式表和javascripts。安装gem后,从命令行运行rails generate jquery:datatables:install。您的application.js和application.css文件应如下所示:

的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
*/

答案 1 :(得分:1)

我很确定这与链轮或宝石本身没有任何关系。我有一个像这样的问题,问题出在我的代码上。没有任何我无法确定的信息,请检查以确保您制定正确的条件。看看我遇到此错误时帮助我的其他堆栈overflow response。如果有机会,您也可以在此视图页面上发布控制器方法。

答案 2 :(得分:1)

运行生成器以生成可数据文件。

rails generate jquery:datatables:install

答案 3 :(得分:-1)

首先确保您的应用已连接到数据库。 然后运行

   rake db:migrate

然后重启服务器并检查。

答案 4 :(得分:-3)

我认为你在Windows上的伙伴就是为application.html.erb提出第5行,在文件的第5行和第6行中找到并用默认值替换应用程序字。