HAML - 意外错误 keyword_ensure,预期输入结束

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

语法 haml 有什么问题?我用了2个空格。显示错误:/home/user/myapp/bds/app/views/polls/_voting_form.html.haml:14: 语法错误,意外 keyword_ensure,期待输入结束

= form_tag votes_path, method: :post, remote: true, id: 'voting_form'
  = hidden_field_tag 'poll[id]', @poll.id

  = render partial: 'polls/poll_item', collection: @poll.poll_items, as: :item  

  %p 
    %b Итого голосов: = @poll.votes_count

  - if current_user.voted_for?(@poll)
    %p Вы уже голосовали!
  - else
    = submit_tag 'Голосовать', class: 'btn-bg'

这里有两个错误:

= form_tag votes_path, method: :post, remote: true, id: 'voting_form' # <= missing do
  ....
  %p 
    %b Итого голосов: = @poll.votes_count # <= no valid haml

应该是

= form_tag votes_path, method: :post, remote: true, id: 'voting_form' do
  ....
  %p 
    %b 
      Итого голосов:
      = " #{@poll.votes_count}"

您不能将普通文本添加到需要 = 的 ruby 片段中。我不认为有一个编译器可以解释这个。 所以:

%b= @poll.votes_count # this works
%b Votes count: = @poll.votes_count # this does not

因此:

%b 
  Итого голосов:
  = " #{@poll.votes_count}"

%b= "Итого голосов: #{@poll.votes_count}"

是必经之路。

我看到两个错误:

  • 您在 form_tag 行中遗漏了 do
  • 第二个错误不是阻塞,而是votes_count:要么使用字符串插值,要么在多行上写入(现在它只会将其打印为字符串)

恕我直言,最简单的解决方案是写

%b Итого голосов: #{@poll.votes_count}