2016-03-03 39 views
3

我的Rails 3應用程序的另一個問題。我在Phusion_Passenger 4.0.37的Apache 2.4.7上運行Ruby 1.9.3p484和Rails 3.2.16。Rails 3 - 未定義方法`[]'爲零:application.html.erb中的NilClass

我添加了jquery-datatables-rails寶石,並在將*= 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; 

} 

當我從application.css刪除*= require dataTables/jquery.dataTables錯誤消失。問題是我需要Datatables CSS文件。我擺弄了很多東西,但最終因爲沒有解決我的問題而恢復原狀。任何想法是受歡迎的。

編輯:因爲我不是太熟悉,無論是sprocketssass,這將是去除鏈輪的最佳方式,這似乎是罪魁禍首,並直接編譯所有.scss文件嗎?我應該@import他們全部變成application.css

編輯#2(2016年3月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)

如果我從我的application.css中刪除所有require行,則不會出現錯誤。所以錯誤來自任何require指令。

+0

您是否嘗試將DataTables CSS文件移動到'app/assets/stylesheets /'中,並在'application.css'中包含'* = require_tree .'行? – PJSCopeland

+0

是的,結果相同,錯誤相同。 – Technoh

+0

您是否嘗試將'application.css'更改爲'application.scss',並使用'@ import'指令來包含css文件。請參閱http://sass-lang.com/documentation/file.SASS_REFERENCE.html#import – abookyun

回答

-3

我認爲你是在windows上,它說第5行是爲application.html.erb提出的,在文件查找第5行和第6行,並用默認值替換應用程序字。

+0

我不在Windows上,我不太明白你的建議。你讀過整個問題了嗎?第5行是'stylesheet_link_tag'。 – Technoh

+0

是在該文件中有名稱的應用程序在引用中,像這樣的「應用程序」使其默認爲這樣的「默認」。行#5後將清除它會說#6引發你必須在#6行上做同樣的使「應用程序」爲「默認」 –

+0

對於遲到對不起 –

1

看起來您並未加載資產管道,特別是樣式表和JavaScript。安裝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 
*/ 
+0

如上面第二條評論所述,我嘗試過使用'* = require_tree',但是我有相同的錯誤信息(例如:'nil:NilClass'的'undefined method'[]')。 – Technoh

+0

我編輯完整答案。希望這會對你有用。 –

-1

首先確保你有你的應用程序連接到數據庫。 然後運行

rake db:migrate 

然後重新啓動服務器和檢查。

+0

當我從application.css('* = require_tree','* = require_self'和'* = require')中刪除'* = require'行時,我的網站很好用。這是*不是*數據庫連接問題。 – Technoh

1

我敢肯定,這與鏈輪或寶石本身沒有任何關係。我有一個這樣的問題,而且問題出在我的代碼上。如果沒有更多的信息我無法確定,請檢查以確保您制定正確的條件。看看這個其他堆棧overflow response,當我遇到這個錯誤時幫助了我。如果你有機會,你也可以發佈你的控制器方法來查看這個頁面。

+0

我的控制器方法爲特定頁面是空的,我只顯示視圖文件。同樣,當我從application.css('* = require_tree','* = require_self'和'* = require')中刪除'* = require'行時,我的網站就會很好地工作。 – Technoh

1

運行生成器以生成數據表文件。

rails generate jquery:datatables:install 
+0

這是很久以前完成的,它不能解決'* = require'錯誤。 – Technoh

相關問題