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

时间:2013-10-19 06:17:08

标签: ruby-on-rails ruby ruby-on-rails-3

我是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>  

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

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

4 个答案:

答案 0 :(得分:4)

<!-- <% end %> -->这是做什么的?一个html注释的ERB标记仍将评估。去掉它。如果你想评论ruby代码,请使用#代替<% #end %>

答案 1 :(得分:3)

正确格式化的代码在诊断此类问题(不匹配等)方面有很长的路要走。试试以下内容:

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

答案 2 :(得分:1)

我所看到的错误就是你应该在此之前设置结束

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

所以试着像这样移动它

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

答案 3 :(得分:1)

我认为你有打开和关闭块的顺序混乱。 iffor都是必须在适当的时间关闭的开放式广告块。

Benjamin提到的已注释掉的结束标记实际上很重要,但放错地方并且必须在</tr></table>标记之间关闭for forum in @forums

我准备了一些带有一些重新调整的修改版本,所以我可以更轻松地理解它。但实际上还没有测试过它。

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