【问题标题】:Rails syntax error: unexpected keyword_ensure, expecting keyword_endRails 语法错误:意外的keyword_ensure,期望keyword_end
【发布时间】:2013-07-18 05:36:56
【问题描述】:
<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% @ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
<a href="<%= @ballot_link %>" class="btn btn-primary">Ready</a>

上面的代码似乎导致:

ready.html.erb:13: syntax error, unexpected keyword_ensure, expecting keyword_end
ready.html.erb:15: syntax error, unexpected $end, expecting keyword_end

发生了什么事?这个语法有什么问题?

【问题讨论】:

  • 您发布了文件的第 10 行和第 11 行,而错误引用了第 13 行和第 15 行。您能否在您发布的行周围发布更多上下文?
  • 第 13 行有语法错误,但这里显示的是第 10 行和第 11 行
  • @henrikhodne,没有第 13 行。11 是最后一行。

标签: ruby-on-rails ruby erb


【解决方案1】:

您收到的错误很可能源于尝试执行 if-else 条件,其中您有一个额外的&lt;% end %&gt; 之前 &lt;% else %&gt;。确保您的条件遵循规范的 if-else-end 逻辑,如下所示:

<% if ... %>
    <% @ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
    <a href="<%= @ballot_link %>" class="btn btn-primary">Ready</a>
<% else %>
    ...
<% end %>

【讨论】:

  • 我忘记了&lt;% end %&gt;。谢谢。
  • 注意:Rails 抱怨一个文件,而我丢失的 end 语句在另一个文件中。
  • 我调用了 if 作为块:&lt;% if (condition) do %&gt;...&lt;% end %&gt;,但当然不应该有 do
【解决方案2】:

您正在使用 if 条件。所以你应该结束它。 erb 的基本 if 条件语法是

<% if ...condition.. %>
    Statement
<% end %>

你必须决定你使用什么?是if条件还是if-else条件

在您的情况下,末尾没有 子句,因此您必须添加它。

<h1>Get Ready</h1>
<% if params[:ballot_position].to_i > 1 %>
<p>
Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %>
and switch places with them.
</p>
<p>
Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.
</p>
<% @ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %>
<a href="<%= @ballot_link %>" class="btn btn-primary">Ready</a> 

<% end %>  # this is what you have to add

【讨论】:

    【解决方案3】:

    我的问题是我在使用 link_to 创建链接时忘记结束 do 块。我的错误代码如下所示:

          <%= link_to("#", :class => "example-class") do %>
            Nested HTML goes here
    

    我忘记结束 do 语句。正确的代码如下:

          <%= link_to("#", :class => "example-class") do %>
            Nested HTML goes here
          <% end %>
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多