1

background

I am trying to migrate an old Rails 2 (Ruby 1.8.7) app to Rails 3.0.9 (Ruby 1.9.3) — yes it's a stepping stone to get it to Rails 4 and Ruby 2.2 — and I've hit the following problem.

The original app makes extensive use of an old Active Form gem which we've hacked slightly to support Ruby 1.9.

It mostly works, but there appears to be some issue with how it interacts with the ActionView::Helpers::AssetTagHelper that's part of ActionPack 3.0.9.

In my specific case I have an ActiveForm::DateCalendarSection (built dynamically) which subclasses ActiveForm::Element::Section, which, according to self.class.ancestors, is a subclass of ActionView::Helpers::AssetTagHelper. Looking at the ActiveForm source however there is no mention of AssetTagHelper or asset_tag_helper so how they are actually connected remains a mystery to me.

Problem

Calls to the method image_path result in an error

undefined local variable or method 'config'

The call to image_path is simply a wrapper around a call to compute_public_path in ActionView::Helpers::AssetTagHelper

# File actionpack/lib/action_view/helpers/asset_tag_helper.rb, line 741
def compute_public_path(source, dir, ext = nil, include_host = true)
  return source if is_uri?(source)

  source += ".#{ext}" if rewrite_extension?(source, dir, ext)
  source  = "/#{dir}/#{source}" unless source[0] == //
  source = rewrite_asset_path(source, config.asset_path)

  has_request = controller.respond_to?(:request)
  if has_request && include_host && source !~ %{^#{controller.config.relative_url_root}/}
    source = "#{controller.config.relative_url_root}#{source}"
  end
  source = rewrite_host_and_protocol(source, has_request) if include_host

  source
end

Diving into that with binding.pry it's evident that config is indeed not defined. Likewise controller is also not defined.

Question

What would have changed between Rails 2 and Rails 3, such that methods from ActionView::Helpers::AssetTagHelper can no longer access Rails' config or the current controller?

4

1 回答 1

0

可能是您没有加载足够的 Rails 支持。也许把你的宝石里面的一切都拉进去

require "active_support/all"

或者,看看现代 Rails 如何通过代理连接到配置:

https://github.com/rails/rails/pull/12622/files

于 2015-08-27T07:34:48.347 回答