Rails syntax error: unexpected keyword_ensure, exp

2020-04-08 10:21发布

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

3条回答
We Are One
2楼-- · 2020-04-08 11:03

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
查看更多
forever°为你锁心
3楼-- · 2020-04-08 11:04

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.

查看更多
Luminary・发光体
4楼-- · 2020-04-08 11:15

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 %>
查看更多
登录 后发表回答