【问题标题】:Haml installation in Rails app failingRails 应用程序中的 Haml 安装失败
【发布时间】:2025-11-27 23:20:06
【问题描述】:

我有一个小型 Rails 应用程序,今晚我非常想将所有 erb 转换为 haml 模板。 Haml 文档建议运行 haml --rails /path/to/app 将其作为插件安装(使用系统上已安装的 gem)。

不幸的是,当我尝试为 rails 启动网络服务器时,我收到以下错误:

/code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:443:in `load_missing_constant': uninitialized constant Haml (NameError)
    from /code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:80:in `const_missing'
    from /code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:92:in `const_missing'
    from /code/src/myapp/config/environment.rb:15
    from ./script/../config/../vendor/rails/railties/lib/initializer.rb:111:in `run'
    from /code/src/myapp/config/environment.rb:5
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from /code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /code/src/myapp/vendor/rails/activesupport/lib/active_support/dependencies.rb:156:in `require'
    from /code/src/myapp/vendor/rails/railties/lib/commands/server.rb:84
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
    from ./script/server:3

它在我的环境文件中的一行上呕吐:

# Be sure to restart your server when you modify this file
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
  # Add additional load paths for your own custom dirs
  config.load_paths += %W(
    #{RAILS_ROOT}/lib/
  )

  # Specify gems that this application depends on and have them installed with rake gems:install
  config.gem 'twitter'
  config.gem 'newrelic_rpm'

  Haml::Template.options[:format] = :html5
  Haml::Template.options[:attr_wrapper] = '"'

  config.plugins = [ :all ]
  config.active_record.observers = :user_observer
  config.time_zone = 'UTC' # "rake -D time" for all time zone names.
end

ConsumerConfig = YAML.load(File.read(Rails.root + 'config' + 'twitter-auth.yml'))

错误在于尝试设置一些 Haml 选项 (Haml::Template.options[:format] = :html5)。使用script/plugin install 安装haml 插件会产生相同的错误,就像environment.rb 顶部的require haml 一样。不确定它是否有所不同,但 rails 冻结在 vendor/rails

这让我很困惑,如果你能解决这个问题,请帮忙。

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

    如果您将 Haml::Template 配置移到 Initializer 块之后,它会起作用吗?可能是在初始化程序运行之后 Rails 才加载插件。

    【讨论】:

    • 今晚你是我的救星。不知道为什么我没有想到,但这就是问题所在,简单明了。感谢这么棒的图书馆!