56
53

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails] haml-railsがあればerb2hamlは不要です!

Last updated at Posted at 2018-05-22

既存のerbをhamlに一括変換する

erb2hamlは不要

erbファイルをhamlに変換する際に、erb2hamlがよく参照されていますが、実はhaml-railsだけで十分です。

以下は、erb2hamlを使う方法をまとめていますが、haml-railsもインストールしており、デフォルトをHamlにしたいのなら、erb2hamlは不要です。

haml-railsで後から一括変換

コマンドは以下のとおり。ちなみに、Rails5.0からはrakeコマンドをrailsコマンドに統一する流れとなってます。

ターミナル
rails haml:erb2haml

一気にerbファイルも削除したい場合は、

ターミナル
HAML_RAILS_DELETE_ERB=true rails haml:erb2haml

試しに、rails g devise:viewsで生成されるDeviseのerbファイルがある状態でhamlに変換してみます。

ターミナル
$ bundle exec rails haml:erb2haml
--------------------------------------------------------------------------------
Generating HAML for app/views/devise/unlocks/new.html.erb...
Generating HAML for app/views/devise/passwords/edit.html.erb...
Generating HAML for app/views/devise/passwords/new.html.erb...
Generating HAML for app/views/devise/mailer/confirmation_instructions.html.erb...
Generating HAML for app/views/devise/mailer/reset_password_instructions.html.erb...
Generating HAML for app/views/devise/mailer/password_change.html.erb...
Generating HAML for app/views/devise/mailer/email_changed.html.erb...
Generating HAML for app/views/devise/mailer/unlock_instructions.html.erb...
Generating HAML for app/views/devise/shared/_links.html.erb...
Generating HAML for app/views/devise/sessions/new.html.erb...
Generating HAML for app/views/devise/confirmations/new.html.erb...
Generating HAML for app/views/devise/registrations/edit.html.erb...
Generating HAML for app/views/devise/registrations/new.html.erb...
Generating HAML for app/views/layouts/mailer.html.erb...
Generating HAML for app/views/layouts/mailer.text.erb...
--------------------------------------------------------------------------------
HAML generated for the following files:
	app/views/devise/unlocks/new.html.erb
	app/views/devise/passwords/edit.html.erb
	app/views/devise/passwords/new.html.erb
	app/views/devise/mailer/confirmation_instructions.html.erb
	app/views/devise/mailer/reset_password_instructions.html.erb
	app/views/devise/mailer/password_change.html.erb
	app/views/devise/mailer/email_changed.html.erb
	app/views/devise/mailer/unlock_instructions.html.erb
	app/views/devise/shared/_links.html.erb
	app/views/devise/sessions/new.html.erb
	app/views/devise/confirmations/new.html.erb
	app/views/devise/registrations/edit.html.erb
	app/views/devise/registrations/new.html.erb
	app/views/layouts/mailer.html.erb
	app/views/layouts/mailer.text.erb
--------------------------------------------------------------------------------
Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)
y
Deleting original .erb files.
--------------------------------------------------------------------------------
Task complete!
No .erb files found. Task will now exit.

ご覧のとおり、erb2haml無しで変換できました。

その他の使い方

haml-railsgem自体は、RailsでHaml形式をサポートするライブラリですが、他にもデフォルトテンプレートエンジンをERBからHamlに変更する機能もあります。
haml-railsの使い方を以下にまとめておきます。

1. インストール

gemfileにhaml-railsを追記して、bundle installしましょう。
これによって、contoroller、scaffold、mailerを生成する際はerbではなく、hamlが生成されます。
こんな感じで。

ターミナル
$ bundle exec rails g controller welcome index
Running via Spring preloader in process 40826
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  haml  ←ここ!!
      create    app/views/welcome
      create    app/views/welcome/index.html.haml  ←ここ!!
      invoke  rspec
      create    spec/controllers/welcome_controller_spec.rb
       exist    spec/views/welcome
   identical    spec/views/welcome/index.html.haml_spec.rb

基本的にはこれだけでOKですが、続けて以下も実行しましょう。

2. application.html.erbを変換

以下を実行して、アプリケーションレイアウトをhamlに変換しましょう。その後、application.html.erbを削除します。
こうすることで、Railsがapplication.html.erbではなく、application.html.hamlを使用するようになります。

ターミナル
$ bundle exec rails g haml:application_layout convert

Running via Spring preloader in process 9113
Success! app/views/layouts/application.html.haml is created.
Please remove the erb file: app/views/layouts/application.html.erb

ちなみに、haml-railsだけで十分と言っていますが、前提gemとしてhtml2hamlが自動でインストールされており、上の機能は実際のところhtml2hamlを使用しています。

3. 他の既存のerbをhamlに変換

基本的にコントローラ等を生成するとデフォルトでhamlになりますが、冒頭のdevise等はerbで出力される(参考:DeviseのビューをHamlとしてカスタマイズする方法)ので、必要に応じてそれらをhamlに変換します。

既に一度hamlに変換していて、erbが削除されていない場合は、以下のようなメッセージが出力されて上書きするかどうか聞いてきます。
Would you like to overwrite these .haml files? (y/n)

ちなみに、こっちも裏ではhtml2hamlが動いています。

参考

備忘録

テンプレートエンジンについては、Railsガイドを参照
と言っても自分もまだ初心者なので勉強しないと。。。

56
53
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
56
53

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?