Ruby-on-rails 默认情况下哈姆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9187209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:47:02  来源:igfitidea点击:

haml by default

ruby-on-railshamlruby-on-rails-3.2

提问by rudolph9

Is there a way to configure rails to use haml by default, i.e. when a scaffold is generated the according scaffold_name/index.html.hamlis generated instead of scaffold_name/index.html.erb.

有没有办法将 rails 配置为默认使用 haml,即当生成脚手架时,会生成相应scaffold_name/index.html.haml的而不是scaffold_name/index.html.erb.

Similar to how you are able to add config.sass.preferred_syntax = :sassto config/application.rband have scaffold_name.sassgenerated by default.

类似于您如何能够补充config.sass.preferred_syntax = :sassconfig/application.rb,并已scaffold_name.sass默认生成的。

Tried adding the following to config/application.rb

尝试将以下内容添加到 config/application.rb

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

but ened up with the following

但结果如下

$ 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

I created a nice little bundle command to replace all erb with haml files following thisscreencast but I'm still interested in making it default when the scaffold is created! How do I make it so haml files (not erb!) are generated by default?

截屏视频之后,我创建了一个漂亮的小包命令,用 haml 文件替换所有 erb,但我仍然有兴趣在创建脚手架时将其设为默认值!如何使默认情况下生成 haml 文件(不是 erb!)?

回答by raymondralibi

I use gem 'haml-rails', '= 0.3.4'in my gemfile. it automatically generates *.html.hamlwithout any configuration.

gem 'haml-rails', '= 0.3.4'在我的 gemfile 中使用。它自动生成,*.html.haml无需任何配置。

回答by Nick Veys

In your application config, try setting the following:

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

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

回答by munna_1

if you have gem 'haml-rails'in your Gemfile it should create haml files by default instead of erb.

如果您的 Gemfile 中gem 'haml-rails',它应该默认创建 haml 文件而不是 erb。

回答by karlingen

This is pretty simple!

这很简单!

All you need to do is to add the following to your Gemfile:

您需要做的就是将以下内容添加到您的 Gemfile 中:

gem 'haml'
gem 'haml-rails'

and then run bundle install

然后运行 bundle install

回答by cevaris

Found this to be the complete solution

发现这是完整的解决方案

Say if you have an Rails Engine project named rails_address

假设您有一个名为的 Rails 引擎项目 rails_address

Add the haml config to lib/rails_address/engine.rb

将 haml 配置添加到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

Added haml deps to rails_address.gemspec

rails_address.gemspec添加了haml deps

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

Lastly require the haml gems in lib/rails_address.rb

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

require "rails_address/engine"
require "haml"
require "haml-rails"

module RailsAddress
end

Execute a bundle installjust incase you have not installed the haml gems yet.

bundle install如果您还没有安装 haml gems,请执行一次。

Now when you generate via scaffold or controller you will create haml views.

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

ex.

前任。

$ 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
...

回答by ekampp

The haml [not found]error is usually because the bundle is incomplete. Have you tried running bundle updateand then rerunning the generator?

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