Rails syntax error: unexpected keyword_ensure, expecting keyword_end

无人久伴 提交于 2019-12-09 03:34:29

问题


<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>

Above code seems to be resulting in:

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

What's going on? What's wrong with this syntax?


回答1:


The errors you're receiving more than likely stem from trying to execute a if-else conditional wherein you have an extra <% end %> before <% else %>. Ensure that your conditional follows canonical if-else-end logic like the following:

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



回答2:


You are using if condition. So you should end it. The basic if conditions syntax for erb is

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

You have to decide what are you using ? It is if condition or if-else condition

In you case, there is not <% end %> clause at end so you have to add it.

<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:


My issue was that I forgot to end a do block when creating a link using link_to. My incorrect code looked like:

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

I had forgotten to end the do statement. The correct code looks like:

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

Hope this helps someone.



来源:https://stackoverflow.com/questions/17715290/rails-syntax-error-unexpected-keyword-ensure-expecting-keyword-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!