Rails语法错误:意外的keyword_ensure,期待keyword_end

Get Ready

1 %>

Voter , go get voter and switch places with them.

Voter , when you are ready, click the button marked "Ready" below.

<a href="https://stackoverflow.com/questions/17715290/rails-syntax-error-unexpected-keyword-ensure-expecting-keyword-end/" class="btn btn-primary">Ready

以上代码似乎导致:

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

这是怎么回事? 这个语法出了什么问题?

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

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

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

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

你必须决定你在用什么? 这是条件或if-else条件

在您的情况下,最后没有<%end%>子句,因此您必须添加它。

 

Get Ready

<% if params[:ballot_position].to_i > 1 %>

Voter <%= params[:ballot_position].to_i - 1 %>, go get voter <%= params[:ballot_position] %> and switch places with them.

Voter <%= params[:ballot_position] %>, when you are ready, click the button marked "Ready" below.

<% @ballot_link = "/vote/#{params[:election_id]}/ballot/#{params[:ballot_position]}" %> Ready <% end %> # this is what you have to add

我的问题是我在使用link_to创建链接时忘了结束do块。 我的错误代码看起来像:

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

我忘了结束做声明。 正确的代码如下:

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

希望这有助于某人。