首页 文章

为什么我的数据库中的所有内容(创建和更新时间,topic_id,post_id)都显示在我的视图中?

提问于
浏览
0

我是编程新手,已经学习Ruby on Rails大约6周了 . 我已经在我的应用程序中添加了评论功能,并且正在正确显示评论时,与评论相关联的(sqlite3)数据库中的其他内容也是如此 - created_at,updated_at,comment_id,post_id . 显示注释的部分具有以下代码:

<%= form_for [post, comment] do |f| %>
  <p><%= @comments.each do |comment| %></p>
  <small>
  <p><%= comment.body %></p>
  </small>
  <% end %> 
<% end %>

正如你所看到的,我只是试图显示评论体,但我正在显示所有内容 .

以下是注释控制器的create方法:

def create  
   @post =  Post.find(params[:post_id])
   @comment = current_user.comments.build(params_comment)
   @comment.post = @post
   authorize @comment

   if @comment.save
     flash[:notice] = "Comment was created"
     redirect_to [@post.topic, @post]
   else 
     flash[:error] = "Comment failed to save"
     redirect_to [@post.topic, @post]
   end
  end
end

我不知道为什么每一个都在显示我是否只是在打电话 . 我研究了这个问题,但没有找到任何东西 . 任何帮助,将不胜感激 .

1 回答

  • 0

    这是修复: -

    <%= form_for [post, comment] do |f| %>
      <!-- Here I removed `=` from `<%` %> -->
      <p><% @comments.each do |comment| %></p> 
      <small>
      <p><%= comment.body %></p>
      </small>
      <% end %> 
    <% end %>
    

    __50709_在块完成完整迭代时返回集合 . 现在,您使用 <%= ..%> ,然后打印 #each 的返回值 . 但是如果 <%..%> ,虽然 @comments.each 仍然返回 @comments 集合,但它不会打印 .

相关问题