2013-10-19 86 views
7

我是一个新手在rails中,我试图创建一个基于教程的论坛应用程序。这是我的论坛网页,但我不断收到错误:Rails的语法错误:意外的keyword_ensure,期待输入结束

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, "https://stackoverflow.com/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> 

回答

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

不,我根据指定进行了更改,但我得到了同样的错误。 – trialError

4

<!-- <% end %> -->这是什么做的?一个html评论ERB标签仍然会评估。去掉它。如果你想评论ruby代码使用#而不是像<% #end %>

+0

当我进行更改时,现在的错误是意外的tCONSTANT。我意识到这是因为没有终止的字符串常量。但是,我没有发现任何未终止的字符串常量。对此有何建议? – trialError

+0

或者,更自然的Ruby评论:<%# end %> –

1

我认为你已经打开和关闭块的顺序混乱起来。 iffor都必须在适当的时候关闭。

本杰明提到的注释掉的结束标记实际上很重要,但放在了错误的位置,必须在</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, "https://stackoverflow.com/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> 
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, "https://stackoverflow.com/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 %> 
相关问题