从控制台访问 image_tag 助手 nil 错误

发布于 2024-11-16 20:28:44 字数 1177 浏览 4 评论 0原文

我正在尝试从控制台访问 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'

I'm trying to access image_tag helper from console.

(in ruby 1.9.2, rails 3.0.1)

I tryed out solutions

helper.image_tag("rails.png")

even

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

I always get the same error

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度° 2024-11-23 20:28:44

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

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

It's nil because config = {}。我相信它是空的,因为在 actionpack-3.0.7/action_view/base.rb controller 的第 220 行中,它是 nil:

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

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

顺便说一句,你为什么要在控制台中执行此操作?似乎将其放入视图并重新加载也同样容易。

I don't have a real answer for you but based on what I've found it doesn't seem to be possible. The nil error is coming from this line because config.assets_dir is nil:

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

It's nil because config = {}. And I believe it's empty because in line 220 in actionpack-3.0.7/action_view/base.rb controller is nil:

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

I know that doesn't help much but if someone can figure out how to override that at console start maybe it'll work.

As an aside, why do you want to do that in the console anyway? Seems it'd be just as easy to put it into a view and hit reload.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文