haml por defecto


Hay una forma de configurar rails para usar haml por defecto, es decir, cuando se genera un scaffold se genera scaffold_name/index.html.haml en lugar de scaffold_name/index.html.erb.

Similar a cómo se puede agregar config.sass.preferred_syntax = :sass a config/application.rb y tener scaffold_name.sass generado por defecto.

, Intentó agregar lo siguiente a config/application.rb

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

Pero con lo siguiente

$ 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

He creado un pequeño y agradable comando bundle para reemplazar todos los archivos erb con haml siguiendo este screencast pero todavía estoy interesado en hacerlo predeterminado cuando se crea el scaffold! ¿Cómo hago para que los archivos haml (no erb! se generan por defecto?

Author: rudolph9, 2012-02-08

6 answers

Utilizo gem 'haml-rails', '= 0.3.4' en mi gemfile. genera automáticamente *.html.haml sin ninguna configuración.

 67
Author: raymondralibi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-09 03:19:09

En la configuración de su aplicación, intente establecer lo siguiente:

config.generators do |g|
  g.template_engine :haml
end
 12
Author: Nick Veys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-08 02:59:00

Si tiene la gema 'haml-rails' en su Gemfile, debería crear archivos haml por defecto en lugar de erb.

 8
Author: munna_1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-09 19:47:04

Esto es bastante simple!

Todo lo que necesita hacer es agregar lo siguiente a su Gemfile:

gem 'haml'
gem 'haml-rails'

Y luego ejecutar bundle install

 6
Author: karlingen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-05 14:56:00

Encontró que esta era la solución completa

Digamos que si tienes un proyecto de motor Rails llamado rails_address

Añade la configuración de haml a 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

Se ha añadido haml deps a 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'
...

Finalmente requiere las gemas haml en lib/rails_address.rb

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

module RailsAddress
end

Ejecuta un bundle install en caso de que aún no hayas instalado las gemas haml.

Ahora, cuando genere a través de scaffold o controller, crear vistas 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
...
 1
Author: cevaris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-12 21:51:08

El error haml [not found] suele deberse a que el paquete está incompleto. ¿Ha intentado ejecutar bundle update y luego volver a ejecutar el generador?

 0
Author: Emil Kampp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-08 11:51:57