Rails syntax error: unexpected keyword_ensure awaiting input completion

I am new to rails and I tried to create a tutorial based forum application. This is my forum page, but I keep getting the error:

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 page that throws an 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> 
+7
ruby ruby-on-rails ruby-on-rails-3
source share
4 answers

<!-- <% end %> --> what does this do? the html commented ERB tag will still be evaluated. Take it away. if you want to comment on ruby ​​code, use # instead, for example <% #end %>

+4
source share

Correctly formatted code is of great importance for diagnosing such problems (inconsistency, etc.). Try 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 %> 
+3
source share

all I see is wrong is to set the end before it appears here

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

so try moving it like this:

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

I think you have an order for opening and closing blocks. if , for - all opening blocks that need to be closed at the corresponding time points.

The enclosed end tag that Benjamin mentioned is really important, but inappropriate and must go between the </tr> and </table> tags to close for forum in @forums .

I prepared a modified version with some reorganizations, so I could more easily understand this. In fact, they did not test it.

 <% 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> 
+1
source share

All Articles