【发布时间】: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