从console nil错误访问image_tag helper

时间:2011-06-23 13:11:06

标签: ruby-on-rails helper

我正在尝试从控制台访问image_tag帮助程序。

(在ruby 1.9.2中, rails 3.0.1

我尝试了解决方案

helper.image_tag("rails.png")

甚至

foo = ActionView::Base.new
foo.image_tag "rails.png"

我总是得到同样的错误

TypeError: can't convert nil into String
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:790:in `join'
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:790:in `rails_asset_id'
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:813:in `rewrite_asset_path'
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:742:in `compute_public_path'
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:561:in `image_path'
from /home/tomi/.rvm/gems/ruby-1.9.2-p180@lovethis/gems/actionpack-3.0.1/lib/action_view/helpers/asset_tag_helper.rb:631:in `image_tag'

1 个答案:

答案 0 :(得分:4)

我没有真正的答案,但根据我发现它似乎不可能。 nil错误来自此行,因为config.assets_dir为nil:

path = File.join(config.assets_dir, source)

它是零,因为config = {}。而且我认为它是空的,因为在actionpack-3.0.7 / action_view / base.rb controller中的第220行是nil:

config = controller && controller.respond_to?(:config) ? controller.config : {}

我知道这没什么用,但是如果有人能够弄清楚如何在控制台启动时覆盖它,也许它会起作用。

顺便说一句,为什么你还想在控制台中这样做呢?似乎将它放入视图并点击重新加载就好了。