rudolph9
rudolph9

Reputation: 8119

haml by default

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.haml is generated instead of scaffold_name/index.html.erb.

Similar to how you are able to add config.sass.preferred_syntax = :sass to config/application.rb and have scaffold_name.sass generated by default.

Tried adding the following to 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 this screencast 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?

Upvotes: 49

Views: 22111

Answers (6)

cevaris
cevaris

Reputation: 5794

Found this to be the complete solution

Say if you have an Rails Engine project named rails_address

Add the haml config to 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

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

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

module RailsAddress
end

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

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

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

Upvotes: 1

karlingen
karlingen

Reputation: 14625

This is pretty simple!

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

gem 'haml'
gem 'haml-rails'

and then run bundle install

Upvotes: 7

munna_1
munna_1

Reputation: 131

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

Upvotes: 8

raymondralibi
raymondralibi

Reputation: 1953

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

Upvotes: 70

ekampp
ekampp

Reputation: 1959

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

Upvotes: 0

Nick Veys
Nick Veys

Reputation: 23939

In your application config, try setting the following:

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

Upvotes: 17

Related Questions