【问题标题】:unexpected kENSURE, expecting $end - Rails/Cucumber/Haml意外的 kENSURE,期待 $end - Rails/Cucumber/Haml
【发布时间】:2010-04-21 22:03:24
【问题描述】:

Rails/Cucumber/Haml 遇到问题。以下 Haml 文件在开发和实时环境中完美运行,但在 Cucumber 中失败并出现以下错误:

/app/views/competitions/show.haml:30: syntax error, unexpected kENSURE, expecting $end (ActionView::TemplateError)
On line #30 of app/views/competitions/show.haml

第 30 行是文件的结尾。它适用于 Haml gem 版本 2.2.3,但不适用于更高版本(我尝试过 2.2.23、2.2.22、2.2.17)

- title @competition.name

%h1
  =h @competition.name

- if @competition.image?
  #main_image
    = image_tag(@competition.image_url)

= RedCloth.new(@competition.description).to_html

%h2
  =h @competition.question

%p 
  - if @competition.running?
    = link_to 'Enter competition', enter_path(:id => @competition.secret)
  - else
    = case @competition.state
      - when 'scheduled' then 'Competition has not opened'
      - when 'closed' then 'Competition closed'

if can? :update, @competition
  = link_to 'Edit', edit_competition_path(@competition)
  |

- if can? :show_stats, @competition
  = link_to 'View stats', competition_stats_path(@competition)

有什么想法吗?

【问题讨论】:

  • 如果没有实际代码,很难说出发生了什么。尝试暂时删除代码块,一次一个,看看有什么效果。开始缩小范围。例如,尝试删除 %p 块下的所有内容,包括 %p 块,然后重新运行测试。
  • 在第 24 行,您的意思是要通过在 if can? :update, @competition 前面加上一个连字符来转义以将其解释为 ruby​​ 代码吗? - if can? :update, @competition.
  • 在调用此 HAML 文件之前,您是否有可能有一个 ensure..end 异常处理块?在我看来,ensure 在没有适当的end 的情况下被调用,而 HAML 文件恰好在错误的时间出现在错误的位置。该错误可能与此文件无关。
  • 除了斯蒂芬指出的问题之外,该错误通常是由开放式条件引起的。也就是说,一个像 if ("- if 1 == 1") 这样的条件,如果它的计算结果为真,它下面没有任何缩进来执行。如果 HAML 的条件下没有任何东西可以使用,那么它不会为条件生成“结束”。检查您的每个条件块,以确保如果/当它们为真时确实发生了某些事情。

标签: ruby-on-rails cucumber haml


【解决方案1】:

根据我使用 cucumber+haml 的经验,这类事情最常见的原因是方法调用中的歧义以及 haml 在条件句中丢失了范围。尝试在所有方法周围加上括号。

在您发布的代码中,第一个if 也缺少它的-

我还建议尝试将 when case 部分放入帮助程序中,因为 haml 有时会与此类内容混淆,尤其是在使用 - 设置时。

您运行的是哪个版本的 Rails?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多