Rails,devise和Haml:语法错误,意外的kELSE

时间:2012-04-07 13:20:08

标签: ruby-on-rails ruby-on-rails-3 devise haml

我正在尝试设置一个运行多个博客的简单应用程序,我的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的想法,所以我想试一试。

提前致谢

2 个答案:

答案 0 :(得分: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)

haml中可以省略end标记。

答案 1 :(得分: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)

它会起作用。哈姆尔将自行关闭。

<强> PS
也可以在-=等不可打印的代码行上使用if(破折号)代替else