すべての夜、
私はここ数時間、頭をかいてここに座っていました。
記事モデルに非常に単純なコメントモデルを添付しています。問題は、すべての記事のコメントセクションの最後に空白のコメントがあるようです。キャピタライズのようなメソッドを使用しようとすると、「capitalize on nil class」でエラーになります。また、コメントごとに背景が灰色のdivにコメントを入れると(Facebookスタイル)、記事の最後に空白のボックスが表示されます。コメント。誰かが何が起こっているのか知っていますか?
とにかくここにコードがあります:コメントコントローラー
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
より多くのコードが必要な場合は、アンディに事前に感謝します。