Ruby-on-rails Rails 语法错误:意外的keyword_ensure,期望输入结束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19463053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:54:01  来源:igfitidea点击:

Rails syntax error : unexpected keyword_ensure, expecting end-of-input

ruby-on-railsrubyruby-on-rails-3

提问by trialError

I am a newbie in rails and i tried creating a forum application based on a tutorial. This is my forum page but i keep getting the error:

我是 Rails 的新手,我尝试根据教程创建一个论坛应用程序。这是我的论坛页面,但我不断收到错误消息:

syntax error, unexpected keyword_ensure, expecting end-of-input

Extracted source (around line #33):
30 
31    <p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>  

here is the forum index page which is throwing the error:

这是引发错误的论坛索引页面:

<% title "Forums" %>  

<table>  
  <tr>  
    <th width="70%">Forum</th>  
    <th width="30%">Last Post</th>  
  </tr>  
  <% for forum in @forums %>  
    <tr>  
      <td><h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>  
        <small><%= forum.topics.count %> topics</small><br />  
        <%=h forum.description %></td>  
      <td class="right">
      <% if forum.most_recent_post %>
      <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
       ago by 
       <%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
       <% else %>no posts<% end %>
       </td>  
      <% if admin? %>
      <td><%= link_to "Edit", edit_forum_path(forum) %>
  <% end %></td>
  <!-- <% end %> -->  
  <% if admin? %>
  <td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
  <% end %>  
</tr>  
  <% end %>  

<p><% if admin? %><%= link_to "New Forum", new_forum_path %><% end %></p>  

回答by Benjamin Udink ten Cate

<!-- <% end %> -->what is this doing? a html commented ERB tag will still evaluate. Remove it. if you want to comment ruby code use #instead, like <% #end %>

<!-- <% end %> -->这是在做什么?html 注释的 ERB 标签仍将评估。去掉它。如果您想评论 ruby​​ 代码,请#改用,例如<% #end %>

回答by Jonathan Bender

Properly formatted code goes a long way towards diagnosing problems like this (mismatch and the like). Try out the following:

正确格式化的代码对诊断此类问题(不匹配等)大有帮助。尝试以下操作:

<% title "Forums" %>  

<table>  
  <tr>  
    <th width="70%">Forum</th>  
    <th width="30%">Last Post</th>  
  </tr>  
  <% for forum in @forums %>  
    <tr>  
      <td>
        <h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>  
        <small><%= forum.topics.count %> topics</small>
        <br />
        <%=h forum.description %>
      </td>
      <td class="right">
        <% if forum.most_recent_post %>
          <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
          ago by 
          <%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
        <% else %>
          no posts
        <% end %>
      </td>  
      <% if admin? %>
        <td><%= link_to "Edit", edit_forum_path(forum) %></td>
        <td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
      <% end %>
    </tr>
  <% end %>
</table>

<% if admin? %>
  <p><%= link_to "New Forum", new_forum_path %></p>
<% end %>

回答by CMW

I think you have the order of opening and closing blocks jumbled up. if, forare all opening blocks that have to be closed at the appropriate times.

我认为您的打开和关闭块的顺序混乱了。 if,for是所有必须在适当时间关闭的打开块。

The commented-out end tag that Benjamin mentioned is actually important but misplaced and has to go between your </tr>and </table>tags to close the for forum in @forums.

Benjamin 提到的注释掉的结束标签实际上很重要但放错了位置,必须在您的</tr></table>标签之间关闭for forum in @forums.

I prepared a modified version with some realignments, so I could make sense of it more easily. Haven't actually tested it, though.

我准备了一个经过一些重新调整的修改版本,所以我可以更容易地理解它。不过还没有实际测试过。

<% title "Forums" %>  

<table>  
  <tr>  
    <th width="70%">Forum</th>  
    <th width="30%">Last Post</th>  
  </tr>  
  <% for forum in @forums %>  
    <tr>  
      <td>
        <h4><%= link_to h(forum.name), forum_path(forum.id) %></h4>  
        <small><%= forum.topics.count %> topics</small><br />  
        <%=h forum.description %></td>  
      <td class="right">
      <% if forum.most_recent_post %>
        <%= distance_of_time_in_words_to_now forum.most_recent_post.last_post_at %>
         ago by 
         <%= link_to forum.most_recent_post.user.username, "/users/#{forum.most_recent_post.last_poster_id}" %>
      <% else %>
        no posts
      <% end %>
      </td>  
      <% if admin? %>
        <td>
          <%= link_to "Edit", edit_forum_path(forum) %>
        </td>
      <% end %>
      <% if admin? %>
        <td><%= link_to "Destroy", forum, :confirm => 'Are you sure?', :method => :delete %></td>
      <% end %>  
    </tr>
  <% end %>
</table>
<p>
  <% if admin? %>
    <%= link_to "New Forum", new_forum_path %>
  <% end %>
</p>

回答by amine

all I could see wrong is that you set endbefore it should here

我所看到的错误是你在它应该在这里之前设置了结束

      <% if admin? %>
      <td><%= link_to "Edit", edit_forum_path(forum) %>
  <% end %></td>

so try to move it like this

所以试着像这样移动它

      <% if admin? %>
      <td><%= link_to "Edit", edit_forum_path(forum) %>
  </td><% end %>