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