2017-04-09 108 views
1

是的,我已经检查过here。它没有工作。部分渲染集合一次太多

的Rails: 5.0.2 红宝石: 2.4.0

我有意见,每次调用时间集合,它呈现一个时间太多和一个空的评论总是出现低于别人当没有评论时,一个空的仍然呈现。

下面是代码:

查看

<h2>Add a comment:</h2> 
<%= render 'comments/form' %> 

<h2>Comments</h2> 
<%= render @video.comments || "There are no comments yet." %> 

表局部

<%= form_for([@video, @video.comments.new]) do |f| %> 
    <p> 
     <%= f.label :name %><br> 
     <%= f.text_field :commenter %> 
    </p> 
    <p> 
     <%= f.label :body %><br> 
     <%= f.text_area :body %> 
    </p> 
    <p> 
     <%= f.submit %> 
    </p> 
<% end %> 

评论部分

<p> 
    <strong>Name:</strong> 
    <%= comment.commenter %> 
</p> 

<p> 
    <strong>Comment:</strong> 
    <%= comment.body %> 
</p> 

<p> 
    <%= link_to 'Destroy Comment', [comment.video, comment], 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %> 
</p> 

控制器

def create 
    @video = Video.find(params[:video_id]) 
    @comment = @video.comments.create(comment_params) 
    redirect_to video_path(@video) 
end 

def destroy 
    @video = Video.find(params[:video_id]) 
    @comment = @video.comments.find(params[:id]) 
    @comment.destroy 
    redirect_to video_path(@video) 
end 

private 
def comment_params 
    params.require(:comment).permit(:commenter, :body) 
end 

有谁知道为什么会呈现局部的多余的时间?

回答

1

您可以尝试致电.scope在评论协会:

<%= render @video.comments.scope || "There are no comments yet." %> 
+0

这实际上并努力消除空的评论,但是现在当没有意见仍然不输出“有没有评论。 “我可以创建一个JavaScript函数来提供这种功能,但我不希望这样做。 –

+0

好吧,有可能通过使用'if-else'条件来渲染评论而不使用javascript来实现这一点:<%if @ video.comments.scope.any? %>等... –