LoginSignup
41
43

More than 5 years have passed since last update.

Haml記法でunexpected keyword_ensure, expecting end-of-inputのsyntax errorが出た時

Last updated at Posted at 2015-11-11

現象

Rails4

haml記法でコードを書いていたときに、下記のような文法エラーが

syntax error, unexpected keyword_ensure, expecting end-of-input

エラーが起きているファイルは、index.html.hamlとのこと。

このsyntax errorは、erbで書いているときは、よく見かけますよね。
だいたい、<% end %>とかが抜けてますよね。

でも、haml記法だと、endとか閉じタグはいらないはずですね。

原因

実は、hamlで上記のようなendの過不足を訴えるエラーが出た時には、
endがないのではなく、doが抜けているときに起きるようなのですね。

例えば、以下のように書くと上記のエラーが出ます!

= link_to photo_path(photo.id)
  = image_tag("#{photo.first.image}")

ですので、

= link_to photo_path(photo.id) do
  = image_tag("#{photo.first.image}")

こう修正します。

その他、form_tag, form_for内のf.text_fieldなどの中に入れ子でコードを書くときには、doを書かないとエラーが出ますね。
form_forとかのdoが抜けてるのはすぐ気付けそうでも、f.text_fieldのdoを抜かしてエラーを出してもなかなか気づかなそうですよね。

閉じタグを気にしなくていいのがhamlの魅力ですが、endはなくて大丈夫でも、doは書き忘れないようにしないといけないんですね。

参考

41
43
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
41
43