Rails 3 - application.html.erb 中 nil:NilClass 的未定义方法“[]”
Posted
技术标签:
【中文标题】Rails 3 - application.html.erb 中 nil:NilClass 的未定义方法“[]”【英文标题】:Rails 3 - undefined method `[]' for nil:NilClass in application.html.erb 【发布时间】:2016-06-17 07:06:37 【问题描述】:我的 Rails 3 应用程序还有另一个问题。我在 Apache 2.4.7 和 Phusion_Passenger 4.0.37 上运行 Ruby 1.9.3p484 和 Rails 3.2.16。
我添加了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>
我的宝石文件:
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 文件。我摆弄了很多东西,但由于它们没有解决我的问题,因此最终将它们还原。欢迎任何想法。
编辑:由于我对sprockets
或sass
都不太熟悉,因此删除似乎是罪魁祸首的链轮并编译所有.scss 文件直接?我应该把@import
他们都变成application.css
吗?
EDIT #2 (2016-03-29):根据 cmets 中的要求,我正在附加(非常长的)完整跟踪:
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
行,则不会出现错误。所以错误来自 any require
指令。
【问题讨论】:
您是否尝试过将 DataTables CSS 文件移动到app/assets/stylesheets/
并在 application.css
中包含 *= require_tree .
行?
是的,同样的结果,同样的错误。
您是否尝试过将application.css
更改为application.scss
,并使用@import
指令来包含css 文件。见sass-lang.com/documentation/file.SASS_REFERENCE.html#import
通常有一个选项可以查看更多回溯——因为这发生在 Rails 内部,所以框架回溯是相关的。自您提出问题 (3.4.0) 以来,还发布了一个新版本的 gem,可能值得一试。
请发布完整的回溯。
【参考方案1】:
您似乎没有加载资产管道,特别是样式表和 javascript。 gem 安装后,从命令行运行rails generate jquery:datatables:install
。您的 application.js 和 application.css 文件应如下所示:
应用程序.js
...
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require turbolinks
//= require_tree .
应用程序.css
...
*= require_tree .
*= require dataTables/jquery.dataTables
*= require_self
*/
【讨论】:
如上面第二条评论中所述,我尝试使用*= require_tree
,但出现相同的错误消息(例如:undefined method '[]' for nil:NilClass
)。
为了完整起见,我编辑了我的答案。希望这对你有用。【参考方案2】:
我很确定这与链轮或宝石本身没有任何关系。前段时间我遇到了这样的问题,问题出在我的代码上。如果没有更多信息,我真的无法分辨,请检查以确保您制作了正确的条件。查看另一个堆栈overflow response,它在我遇到此错误时帮助了我。如果有机会,您还可以发布此视图页面的控制器方法。
【讨论】:
我的特定页面的控制器方法是空的,我只显示视图文件。同样,只要我从 application.css(*= require_tree
、*= require_self
和 *= require
)中删除 *= require
行,我的网站就会运行良好。【参考方案3】:
运行生成器生成数据表文件。
rails generate jquery:datatables:install
【讨论】:
这是很久以前做的,并没有解决*= require
错误。【参考方案4】:
首先确保您的应用已连接到数据库。 然后运行
rake db:migrate
然后重启服务器并检查。
【讨论】:
从我的 application.css(*= require_tree
、*= require_self
和 *= require
)中删除 *= require
行后,我的网站运行良好。这不是数据库连接问题。【参考方案5】:
我想哥们,你在 windows 上说第 5 行是为 application.html.erb 提出的,进入文件找到第 5 行和第 6 行,并用默认值替换应用程序字。
【讨论】:
我不在 Windows 上,我不太明白你的建议。你读过整个问题吗?第 5 行是stylesheet_link_tag
。
是的,在该文件中有一个名为 application 的单词,用引号括起来,如“application”,使其默认为“defaults”。在第 5 行变得清晰后,它会说第 6 行被提出,你必须在第 6 行做同样的事情,也将“应用程序”设置为“默认值”
抱歉迟到了
如果我改变它,那么我的 application.css 不会加载,CSS 树(数据表 CSS 文件)也不会加载。我希望加载所有这些文件而无需手动添加 stylesheet_link_tag。如果我从文件中删除require
行,我可以加载所有application.css
文件,但如果我这样做,那么数据表CSS 文件显然不会加载。并不是说 javascript require
可以按预期工作并加载完整的数据表 javascript 树。以上是关于Rails 3 - application.html.erb 中 nil:NilClass 的未定义方法“[]”的主要内容,如果未能解决你的问题,请参考以下文章
PhoneGap 和 rails 3:如何与 rails 3 应用程序交互
Rails:“bundle install”由于旧的 rails 版本(3.2)而失败
Rails 3.2.3 GeoLocation 使用 MaxMind