Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在具有0值的实例上使用每个方法时打印一次_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 在具有0值的实例上使用每个方法时打印一次

Ruby on rails 在具有0值的实例上使用每个方法时打印一次,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我在我的帖子上有一些基本的评论。除了一个部分,它工作得很好,即使有0条注释,也会生成一条空注释,至少是HTML部分 查看:posts/show.html.erb: <% @post.comments.each do |comment| %> <div class="comment"> Comment: <%= comment.content %> <%= link_to "delete", comment, meth

我在我的帖子上有一些基本的评论。除了一个部分,它工作得很好,即使有0条注释,也会生成一条空注释,至少是HTML部分

查看:posts/show.html.erb:

<% @post.comments.each do |comment| %>
   <div class="comment">
     Comment:
     <%= comment.content %> 
     <%= link_to "delete", comment, method: :delete  %>
   </div>
<% end %>
检查屏幕截图。请注意,注释:0,但打印了一个空实例

下面是针对此特定问题的测试:

  def setup
    @user = users(:kunok)
    @post = posts(:food)
  end

  def go_to_post(logged_in = false)
    log_in_as @user if logged_in
    get post_path @post
    assert_template 'posts/show'
  end

  test "if there is only 1 comment, there should be only 1 comment displayed" do
    go_to_post true
    assert @post.comments.count == 1 # pass
    assert_select 'div.comment', count: 1 # failure
  end
导致这一失败的原因是:

1) 失败: PostCommentsTest测试如果只有评论,那么应该只有评论显示[/home/kunok/dev/food social app/test/integration/post\u comments\u test.rb:29]: 应正好有1个元素与“div.comment”匹配,找到2。。 预期:1 实际:2

实际上,通过在@post.comments集合中添加新注释来更改@post.comments集合。为了避免这个问题,您可以直接实例化一个新注释

@comment = Comment.new
实际上,通过在@post.comments集合中添加新注释来更改@post.comments集合。为了避免这个问题,您可以直接实例化一个新注释

@comment = Comment.new
@comment = Comment.new