哈默默认

有没有一种方法来configurationrails默认使用哈姆,即当一个脚手架生成时,会产生scaffold_name/index.html.haml而不是scaffold_name/index.html.erb

类似于您可以将config.sass.preferred_syntax = :sass添加到config/application.rb并且默认生成scaffold_name.sass

尝试添加以下config/application.rb

 config.generators do |g| g.template_engine :haml end 

但与以下结果

 $ rails generate scaffold foo name:string invoke active_record create db/migrate/20120208152550_create_foos.rb create app/models/foo.rb invoke test_unit create test/unit/foo_test.rb create test/fixtures/foos.yml route resources :foos invoke scaffold_controller create app/controllers/foos_controller.rb error haml [not found] invoke test_unit create test/functional/foos_controller_test.rb invoke helper create app/helpers/foos_helper.rb invoke test_unit create test/unit/helpers/foos_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/foos.js.coffee invoke sass create app/assets/stylesheets/foos.css.sass invoke sass identical app/assets/stylesheets/scaffolds.css.sass $ rails destroy scaffold foo invoke active_record remove db/migrate/20120208152550_create_foos.rb remove app/models/foo.rb invoke test_unit remove test/unit/foo_test.rb remove test/fixtures/foos.yml route resources :foos invoke scaffold_controller remove app/controllers/foos_controller.rb error haml [not found] invoke test_unit remove test/functional/foos_controller_test.rb invoke helper remove app/helpers/foos_helper.rb invoke test_unit remove test/unit/helpers/foos_helper_test.rb invoke assets invoke coffee remove app/assets/javascripts/foos.js.coffee invoke sass remove app/assets/stylesheets/foos.css.sass invoke sass 

我创build了一个很好的小包命令来replace所有的这个截屏video中的哈姆文件,但我仍然有兴趣使默认脚手架创build时! 我怎样才能让haml文件(不是erb!)默认生成?

gem 'haml-rails', '= 0.3.4'使用gem 'haml-rails', '= 0.3.4' 。 它会自动生成*.html.haml而无需任何configuration。

在您的应用程序configuration中,尝试设置以下内容:

 config.generators do |g| g.template_engine :haml end 

如果你的gemfile中gem'haml-rails' ,它应该默认创buildhaml文件,而不是erb。

这很简单!

所有你需要做的是将以下内容添加到你的Gemfile:

 gem 'haml' gem 'haml-rails' 

然后运行bundle install

haml [not found]错误通常是因为该包不完整。 您是否尝试过运行bundle update ,然后重新运行生成器?

发现这是完整的解决scheme

说你是否有一个名为rails_address的Rails引擎项目

将hamlconfiguration添加到lib / rails_address / engine.rb

 module RailsAddress class Engine < ::Rails::Engine isolate_namespace RailsAddress config.generators do |g| g.template_engine :haml end end end 

增加了haml deps到rails_address.gemspec

 ... s.add_dependency "rails", "~> 4.1.10" s.add_dependency 'haml', '~> 4.0.6' s.add_dependency 'haml-rails', '~> 0.9.0' ... 

最后需要lib / rails_address.rb中的haml gems

 require "rails_address/engine" require "haml" require "haml-rails" module RailsAddress end 

执行一个bundle install只是因为你还没有安装哈姆gem。

现在,当您通过脚手架或控制器生成时,您将创buildhaml视图。

恩。

 $ rails g scaffold Address street:string city:string state:string zip_code:string ... invoke haml exist app/views/rails_address/addresses create app/views/rails_address/addresses/index.html.haml create app/views/rails_address/addresses/edit.html.haml create app/views/rails_address/addresses/show.html.haml create app/views/rails_address/addresses/new.html.haml create app/views/rails_address/addresses/_form.html.haml ...