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 回答