HTMLからHAMLへの一括変換で「rake haml:replace_erbs」が使えない場合

40日目です。
題名の通り、本日の学習でhtmlファイルをhamlに一括変換するという指示があり、
「rake haml:replace_erbs」をターミナルに打ち込んだのですが、
「rake aborted!」(rakeが中止されました)と怒られました。

結論

ターミナルで「rake -T」で調べてみてください。
「rake haml:erb2haml 」が出てくると思います。

解決までの流れ

まず、「rake aborted!」以下のエラー文を見ます。
Don't know how to build task 'haml:replace_erbs'
(See the list of available tasks with rake --tasks)

訳:タスク 'haml:replace_erbs'の作り方がわからない
rake --tasksで利用可能なタスクのリストを見てください)

なるほど、「rake routes」のような確認できるコマンドがあるのね、ということでターミナルに打ち込みました。

ターミナル「rake --tasks」

-bash: rake--tasks: command not found

、、、今度はそんなコマンドはないと怒られてしまいました笑

ということで調べたところ、下記のサイトが参考になりました。

英語の通り、タスク名が見つからない状態です。rake -Tとしてタスクの一覧を確認してみましょう。

Ruby on Rails - rubyで実行するとDon't know how to build task 〜 みたいなエラーが出ます。|teratail

なるほど、「rake --tasks」ではなく「rake -T」なのね、と理解し、実行しました。

ターミナル「rake -T 」


rake haml:erb2haml # Convert html.erb to html.haml each file in app/views

なるほど、「rake haml:replace_erbs」ではダメで、「rake haml:erb2haml 」なら良いのか!ということで実行

ターミナル「rake 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/_error_messages.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/groups/edit.html.erb...
Generating HAML for app/views/groups/new.html.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/_error_messages.html.erb
app/views/devise/confirmations/new.html.erb
app/views/devise/registrations/edit.html.erb
app/views/groups/edit.html.erb app/views/groups/new.html.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.

無事erbファイルからhamlファイルへの変換が完了しました。
変換したファイルは、ハッシュがコロンを用いた最新の書き方ではなく、
ハッシュロケットになってしまっている為、直した方がベターかと思います。

参考:

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


一昨日紹介したブログアプリについてですが、発表会直前に調べていたらMaterialize.cssに対しても中央寄せができました。
(色も変えられたのでより統一感のあるものに変えました。)
before

f:id:tattaka_s:20190802213849p:plain

f:id:tattaka_s:20190802213904p:plain

after

f:id:tattaka_s:20190802213731p:plain

f:id:tattaka_s:20190802213831p:plain

悩んでいた中央寄せの方法は意外と簡単でした。
application.html.erbで読み込んでいた、style.cssファイルに下記を付け足すだけです。

.contents {
  margin: 0 auto;
  width: 800px;
}

.card-panel {
  text-align: center;
}

.card {
  margin: 0 auto;
  width: 600px;
}

.card-panelは「みんなで褒めよう」の部分で、
.cardは「投稿部分」です。

ポイントは、「magin: 0 auto;」ができないときに、「width」を指定することです。

最初「magin: 0 auto;」だけをしていたのですが、変化がありませんでした。
中央寄せは機能しているのですが、ブロック幅が広すぎる故に見た目上中央寄せができていなかったそうです。

下記のサイトが参考になりました。
CSSの中央寄せ「margin: 0 auto;」がうまくいかないとき | 西沢直木のIT講座

※一昨日のブログでも載せましたが、
投稿の文言などは、個人的に好きな、なんでも肯定してくれる
人気キャラクターの「コウペンちゃん」の言葉を使わせて頂きました
作者:るるてあ(さん)

るるてあ (@k_r_r_l_l_) | Twitter


明日は弟の引越しを手伝うので学習は進まないと思いますが、
弟はSEなので参考になる情報を色々聞き出して来たいと思います。