意外的keyword_ensure,期待输入结束

时间:2014-05-06 19:20:16

标签: html ruby-on-rails-4

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

3 个答案:

答案 0 :(得分:3)

您没有关闭form_for中的第一个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're missing this tag

答案 1 :(得分:2)

尝试将render'comments/form'替换为render('comments/form')。或者尝试在函数名称和参数之间添加一个空格,如下所示:render 'comments/form'

答案 2 :(得分:0)

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