【问题标题】:Rails 3 not loading HAML handlerRails 3 未加载 HAML 处理程序
【发布时间】:2011-09-07 20:22:07
【问题描述】:

我在我的应用程序中遇到了 Rails 3 和 HAML 的一些问题:出于某种原因,Rails 似乎没有加载处理程序来处理 haml 文件。每个动作都会给出一个类似下面的错误信息:


模板丢失

缺少带有 {:formats=>[:html], :handlers=>[:rjs, :rhtml, :rxml, :builder, :erb], :locale=>[:en, :en 的模板contact_search/index ]} 在视图路径“/var/www/osphonebook/app/views”、“/var/www/osphonebook/vendor/bundle/ruby/1.8/gems/devise-1.3.4/app/views”中


查看“处理程序”选项:它没有 :haml...

问题是,这只发生在我公司设置的服务器上的生产模式下。在开发和测试模式下它工作正常。此外,如果我在开发 PC 上以生产模式启动应用程序,它就可以工作。

关于服务器的一些信息:

更新(2011 年 6 月 6 日):升级到 Ruby 1.9,但仍然无法正常工作。

ruby 1.9.2p0 (2010-08-18 revision 29036) [i486-linux]

Gems included by the bundle:
abstract (1.0.0)
actionmailer (3.0.7)
actionpack (3.0.7)
activemodel (3.0.7)
activerecord (3.0.7)
activeresource (3.0.7)
activesupport (3.0.7)
arel (2.0.10)
bcrypt-ruby (2.1.4)
builder (2.1.2)
bundler (1.0.14)
devise (1.3.4)
erubis (2.6.6)
haml (3.1.1)
i18n (0.5.0)
kgio (2.4.1)
mail (2.2.19)
mime-types (1.16)
orm_adapter (0.0.5)
pg (0.11.0)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.7)
railties (3.0.7)
rake (0.8.7)
sass (3.1.2)
sqlite3 (1.3.3)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.27)
unicorn (3.6.2)
warden (1.0.4)

如果需要更多信息,请评论问题,我会更新它。感谢您的帮助。

【问题讨论】:

  • 对于您发布的错误,您的视图模板的确切文件名是什么?
  • 另外,您是否更新了您的捆绑包并在您的生产机器上重新启动了服务器?
  • @dmarkow 感谢 cmets。文件名是 app/views/contact_search/index.html.haml 是的,bundle 之后服务器重启了。
  • 不要将 haml 放在 :assets 组中。 默认情况下,资产组中的 Gem 不包含在生产中。 我知道你已经找到了解决方案,我只是希望阻止有人和我落入同样的陷阱。
  • 同样,不要只将 haml-rails 放在 :development Gemfile 组中,我正在运行自动化测试,所以我也需要在 :test 中包含 haml-rails。

标签: ruby-on-rails-3 haml actionview


【解决方案1】:

尝试使用 gem haml-rails

【讨论】:

  • 我看到人们成功使用 gem 'haml' 和 gem 'haml-rails'。我认为它们是相同的,但官方文档说要使用 gem 'haml'。
  • 他们不一样。 Haml-rails 提供生成器并在 Rails 中激活 HAML。没有它应该可以工作,但使用它可能是解决他的问题的快速解决方案。
  • 谢谢你的建议,我刚试过,可惜没用。 “模板丢失”错误仍然存​​在。 @Trenton 从我在 GitHub 上的源代码中看到,haml-rails 添加了生成器并为真正的 haml gem 设置了一些基本选项。
  • 在重写一整天的代码部分后,20 种不同的方法尝试了所有类型 - 这解决了我在 Heroku 上的问题!我无法在本地复制的东西 --- 谢谢 Michaël
【解决方案2】:

我发现了问题:我更改了config/environments/production.rb 文件来为ActionMailer 设置一些个性化代码。问题是我直接使用该类,如下所示:

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.charset = "utf-8"

而不是这样做:

config.action_mailer.delivery_method = :sendmail
config.action_mailer.raise_delivery_errors = true
config.action_mailer.charset = "utf-8"

似乎使用 ActionMailer 类直接触发了 ActionView 加载器,并设置了所有内部变量,从而阻止了 HAML 代码自行安装。

更改代码后,它就像一个魅力。

【讨论】:

  • @Sam 我在某处看到新的“config.action_mailer”sintax 比旧的更受欢迎,直接在类中设置配置。但是在尝试了这么多事情让它发挥作用之后,主要是运气和绝望......
【解决方案3】:

我找到了在生产模式下运行时出现“缺少 HAML 模板”错误的解决方案 (使用 Rails 3.2.6 和 haml-rails 0.3.4):

/config/application.rb 中有

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

我把它改成了

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  # Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  Bundler.require(:default, :assets, Rails.env)
end

现在它可以工作了。

【讨论】:

  • 感谢您的提示。我在使用虚拟应用程序开发的引擎中遇到了问题,并且在 Gemfile 中的一个组中有 haml gem。我将Bundler.require 更改为Bundler.require(:default, Rails.env, :dummy_app) 并解决了它。
【解决方案4】:

添加

require "haml"

config/test.rb(和/或development.rbproduction.rb)为你解决这个问题?

(请注意,我使用的是 Rails 3.2.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2011-10-14
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多