![](/img/trans.png)
[英]Rails : syntax error, unexpected keyword_ensure, expecting $end
[英]Rails: syntax error, unexpected keyword_ensure, expecting $end
我正在創建一個非常基本的rails應用程序(學習教程),並且無法理解為什么我收到此錯誤。 我試過故障排除但無濟於事。
我的代碼:
<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>
一切都很好,直到我添加了Else語句,但不知道我的錯誤是什么 - 我確定這是一個非常小的語法修復,但你的幫助將非常感激。
您的代碼中存在錯誤。
在else
開始之前,你的代碼中有額外的end
。
刪除它可以解決您的問題......
正如其他人所指出的,代碼中的錯誤是if
和else
塊之間的額外<% end %>
。
在我看來,真正的解決方案是將以下內容添加到您的Gemfile:
group :development do
gem 'better_errors'
end
(當您看到由語法問題導致的此錯誤消息時,這通常是真正的解決方案。)
刪除<% else %>
之前的<% end %>
<% else %>
。
代碼應如下:
<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>
在我來說,我忘了do
一個循環:
<% @things.each |thing| %>
正確的格式是:
<% @things.each do |thing| %>
<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>
它應該是這樣的
如果你使用HAML,這可能是原因:
- model_class = Products
.products-page
什么會解決? Identation。
- model_class = Products
.products-page
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.