晚报所有,简单的Rails留言中加入额外的空白评论
我坐在这里抓我的头超过这个最后几个小时。
我有一个非常简单的评论模型附加到文章模型。问题是,每篇文章评论部分末尾似乎都有一个空白的评论。如果我尝试使用像“利用nil类”这样的错误方法,并且如果我将每个评论(facebook样式)的灰色背景的div中的注释放在一篇文章的末尾出现一个空白框注释。有谁知道发生了什么事?
反正继承人的代码: 评论控制器
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(params[:comment])
if @comment.save
flash[:success] = "Comment created"
redirect_to @article
else
flash[:error] = "Something went wrong"
redirect_to @article
end
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to @article
end
end
意见型号
attr_accessible :name, :content
belongs_to :article
validates_presence_of :article_id
validates_presence_of :content, length: { maximum: 300 }
default_scope order: "comments.created_at DESC"
评论形式
<a href='#', id='comments-form', class="btn btn-large">Add a comment</a>
<div id="comment">
<%= form_for([@article, @article.comments.build]) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :content %>
<%= f.text_field :content %>
<br>
<%= f.submit "Create", class: "btn btn-large" %>
<% end %>
</div>
言论表明
<legend>Comments</legend>
<% comments.each do |comment| %>
<sabon><%= comment.name %></sabon><br>
<p><%= comment.content %></p>
<hr>
<% end %>
文章的底部显示
<%= render partial: "comments/form", locals: { article: @article } %><br><br>
<%= render partial: "comments/show", locals: { comments: @article.comments }%>
路线
resources :articles do
resources :comments
end
任何帮助将是巨大的谢谢你们,提前感谢安迪,如果你需要更多的代码就骂。
您可以检查数据库,看看是否真的有一个空白的评论?或者钢轨只是写一个? – Danpe
表单中的“@ article.comments.build”行构建新的评论对象。然后你在show partial中列出@ article.comments这个空白的评论。 –
将其作为答案将其作为答案休闲和生病upvote和刻度线,欢呼声! – dodgerogers747