2

複数のブログを実行するための単純なアプリケーションをセットアップしようとしていますが、私の app/views/layouts/application.html.haml ファイルは次のようになります。

!!!
%html
  %head
    %title Brimble's Blogs
    = stylesheet_link_tag    "application", :media => "all"
    = javascript_include_tag "application"
    = csrf_meta_tags

  %body
    %p.notice= notice
    %p.alert= alert

  .user-auth-nav{style => 'float:right'}
    = if user_signed_in?
      = link_to('Edit registration', edit_user_registration_path)
      = link_to('Logout', destroy_user_session_path)
    = else
      = link_to('Login', new_user_session_path)
      = link_to('Register', new_user_registration_path)
    = end

= yield

私が得るエラーは次のとおりです。

compile error
<myapp>/app/views/layouts/application.html.haml:18: syntax error, unexpected kELSE
<myapp>/app/views/layouts/application.html.haml:22: syntax error, unexpected kEND
<myapp>/app/views/layouts/application.html.haml:23: unknown regexp options - htl
<myapp>/app/views/layouts/application.html.haml:23: syntax error, unexpected $undefined
));}\n  </div>\n</html>\n#{_hamlout.adjust_tabs(-2); _...
                        ^
<myapp>/app/views/layouts/application.html.haml:25: syntax error, unexpected kENSURE, expecting $end

抽出されたソース (18 行目あたり):

15:       = link_to('Edit registration', edit_user_registration_path)
16:       = link_to('Logout', destroy_user_session_path)
17:     = else
18:       = link_to('Login', new_user_session_path)
19:       = link_to('Register', new_user_registration_path)
20:     = end
21: 

このページのチュートリアルに従っています: http://www.logansbailey.com/2011/02/27/adding-authorization-using-devise/ チュートリアルでは erb を使用していますが、Haml のアイデアが本当に好きなので、試してごらん。

前もって感謝します

4

2 に答える 2

3

何かを出力することになっていない Ruby コードの場合は-、代わりに=次のように使用します。

  .user-auth-nav{style => 'float:right'}
    -if user_signed_in?
      =link_to('Edit registration', edit_user_registration_path)
      =link_to('Logout', destroy_user_session_path)
    -else
      =link_to('Login', new_user_session_path)
      =link_to('Register', new_user_registration_path)

タグは、endhaml では省略できます。

于 2012-04-07T13:23:48.630 に答える
1

Haml をビュー エンジンとして使用している場合は、 を使用しませんend。書き込みのみ

= if user_signed_in?
  = link_to('Edit registration', edit_user_registration_path)
  = link_to('Logout', destroy_user_session_path)
= else
  = link_to('Login', new_user_session_path)
  = link_to('Register', new_user_registration_path)

そして、それはうまくいきます。Haml は自動的にブロックを閉じます。

PS
また、およびのような印刷できないコード行-の代わりに (ダッシュ) を使用します。=ifelse

于 2012-04-07T13:22:51.327 に答える