Rails: syntax error, unexpected keyword_ensure, expecting $end

回眸只為那壹抹淺笑 提交于 2019-12-22 08:33:51

问题


I'm creating a very basic rails app (learning tutorial) and can't understand why I'm getting this error. I've tried troubleshooting but to no avail.

My code:

<ul class = "nav pull-right">
  <% if user_signed_in? %>
    <li><%= link_to current_user.full_name, edit_user_registration_path %></li>
    <li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>
  <% end %> 

  <% else %>

    <li><%= link_to "Log In", new_user_session_path %></li>
    <li><%= link_to "Sign Up", new_user_registration_path %></li>
  <%end%>

</ul>

Everything was working fine, until I added the Else statement, but no idea what my error is - I'm sure it's a very minor syntax fix, but your help would be much appreciated.


回答1:


there has an error in your code.
You have extra end in your code before the else starts.
Removing that may solve your problem...




回答2:


The error in your code, as others have pointed out, is the extra <% end %> between your if and else blocks.

In my opinion, the real solution is to add the following to your Gemfile:

group :development do
  gem 'better_errors'
end

(This will often be the real solution for when you see this error message resulting from syntax issues.)




回答3:


Remove the <% end %> just before the <% else %>.

The code should be as follows:

<ul class = "nav pull-right">
  <% if user_signed_in? %>
    <li><%= link_to current_user.full_name, edit_user_registration_path %></li>
    <li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>

  <% else %>

    <li><%= link_to "Log In", new_user_session_path %></li>
    <li><%= link_to "Sign Up", new_user_registration_path %></li>
  <%end%>

</ul>



回答4:


If you'd use HAML, this could be the cause:

- model_class = Products
  .products-page

What will solve? Identation.

- model_class = Products
.products-page



回答5:


In my case I forgot a do in a loop:

<% @things.each |thing| %>

The correct format is:

<% @things.each do |thing| %>



回答6:


<ul class = "nav pull-right">
  <% if user_signed_in? %>
    <li><%= link_to current_user.full_name, edit_user_registration_path %></li>
    <li><%= link_to "Log Out", destroy_user_session_path, method: :delete %></li>  
  <% else %>
    <li><%= link_to "Log In", new_user_session_path %></li>
    <li><%= link_to "Sign Up", new_user_registration_path %></li>
 <%end%>
</ul>

It should be like this



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

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