[英]unexpected keyword_ensure, expecting end-of-input
我是Ruby on Rails的新手,我試圖創建一個具有群組的網站,群組具有帖子,帖子具有評論,每當我嘗試運行我的代碼時,它都會顯示以下錯誤:
語法錯誤,意外的keyword_ensure,期望輸入結束指向注釋視圖中這部分代碼:
<h2>Add a comment:</h2>
<%= render'comments/form' %>
誰能告訴我我的代碼有什么問題嗎?
這是我的代碼:
組顯示視圖:
<p>
<strong>Name:</strong>
<%= @group.name %>
</p>
<p>
<strong>Description:</strong>
<%= @group.description %>
</p>
<p>
<strong>Interest:</strong>
<%= @group.interest %>
</p>
<h2>Posts</h2>
<% @group.posts.each do |post| %>
<% @post = post %>
<% if @post != nil %>
<%= render 'posts/post', :collection => @group.posts%>
<% end %>
<% end %>
<h2>Add a Post</h2>
<%= render 'posts/new' %>
<%= link_to 'Edit', edit_group_path(@group) %> |
<%= link_to 'Back', groups_path %>
<%= link_to 'Destroy', edit_group_path, method: :delete, data: { confirm: 'Are you sure?' } %>
_post.html.erb:
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<p>
<strong>Text:</strong>
<%= @post.text %>
</p>
<h2>Comments:</h2>
<% @post.comments.each do |comment| %>
<% @comment = comment %>
<% if @comment != nil %>
<%= render 'comments/comment', :collection => @post.comments%>
<% end %>
<% end %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Show', comment %>
<%= link_to 'Edit', edit_group_post_path(p@ost) %>
<%= link_to 'Destroy', edit_group_post_path, method: :delete, data: { confirm: 'Are you sure?' } %>
帖子:_form.html.erb:
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h2>Add a post:</h2>
<%= form_for([@group, @group.posts.build]) do |f| %>
<p>
<%= f.label :Title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :Text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
_comment.html.erb:
<p>
<strong>Commenter:</strong>
<%= @comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= @comment.comment %>
</p>
<%= link_to 'Edit', edit_group_post_comment_path(@comment) %>
<%= link_to 'Destroy', edit_group_post_comment_path, method: :delete, data: { confirm: 'Are you sure?' } %>
評論:_form.html.erb:
<%= form_for([@group,@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<% end %>
您不會關閉posts/_form.html.erb
的第一個form_for
塊。
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %> <---- you're missing this tag
嘗試用render('comments/form')
替換render'comments/form'
。 或嘗試在函數名稱和參數之間添加一個空格,如下所示: render 'comments/form'
。
posts: _form.html.erb:
在本部分中,您使用了兩種形式,但僅關閉了一種形式,即第二種形式。 因此,您需要關閉第一個表單助手。
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<% end %> <!-- You are missing this end in posts: _form.html.erb and also you have another problem -->
comments: _form.html.erb:
在此部分中,您兩次關閉表單,因此只需刪除一個<%end%>
<%= form_for([@group,@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<!-- remove last "end" as it is an extra closing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.