Comments form showing blank comments even when database is empty, rails -
Comments form showing blank comments even when database is empty, rails -
i'm building comments section on blog, , far have working point can submit comments , have them appear on page.
however, there blank comment on page whether have 10 comments or database empty.
*** views/artist/lyrics/show.html.erb *** <%= form_for(@lyric.comments.build, url: artist_album_lyric_comments_path(@artist, @album, @lyric)) |f| %> <%= f.text_area :content %> <%= f.submit "comment" %> <% end %> <% if @lyric.comments.any? %> <% @lyric.comments.each |comment| %> <%= comment.username %> <%= comment.content %> <% end %> <% else %> no 1 has commented. <% end %> *** /controllers/users/comments_controller.rb *** def create @comment = comment.new(comment_params) @comment.user_id = current_user.id @comment.username = current_user.username @comment.lyric_id = lyric.friendly.find(params[:lyric_id]).id if @comment.save redirect_to (:back) else redirect_to root_url end end the comment model nested , think has it. it's artist => album => lyric => comment
when remove comment form page blank comment disappears , <else> statement runs.
in form_for tag, building comment on @lyric. believe showing when calling @lyric.comments after. try:
<% @lyric.comments[0..-2].each |comment| %> that grab comments first sec lastly (basically of them except new 1 created).
edit:
also alter <% if @lyric.comments.any? %> <% if @lyric.comments.any? && !@lyric.comments[0].new_record? %>
ruby-on-rails ruby-on-rails-4
Comments
Post a Comment