Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:21:20+00:00 2026-06-12T05:21:20+00:00

Evening all, I’ve sat here scratching my head for the last few hours over

  • 0

Evening all,

I’ve sat here scratching my head for the last few hours over this.

I have a very simple comments model attached to an article model. Issue is, there seems to be a blank comment at the end of every articles comments section. If i try and use a method like capitalize it errors out with “capitalize on nil class”, also if i put the comments in a div with a grey background for each comment (facebook style) a blank box appears at the end of an articles comments. Does anyone know whats going on?

Anyway heres the code:
Comments controller

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

comments Model

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"

comment form

<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>

comments show

<legend>Comments</legend>

    <% comments.each do |comment| %>
        <sabon><%= comment.name %></sabon><br>
        <p><%= comment.content %></p>
        <hr>
        <% end %>

bottom of article show

<%= render partial: "comments/form", locals: { article: @article } %><br><br>
<%= render partial: "comments/show", locals: { comments: @article.comments }%>

routes

resources :articles do
    resources :comments
  end

any help would be great thanks guys, thanks in advance Andy, if you need more code just shout.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:21:21+00:00Added an answer on June 12, 2026 at 5:21 am

    In the comment form line @article.comments.build creates new Comment object. Rendering of the form happens before rendering the comments/show so the new blank Comment object is present in @article.comments collection.

    UPDATE
    You can exclude newly created object from comments, for example:

    @article.comments.reject(&:new_record?)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been beating my head in the wall all evening on this, can
I've been battering my head against this all evening and can't see where I'm
Good evening, I am tired and have been fighting with this for hours. I
Good evening, I'm having an issue with Masonry. This is all my relevant code:
I have been trying to connect to mysql all evening using PHP. I know
Good evening! Been searching all over, and I'm afraid I've just got something completely
Evening all. I have the following code that I need looking into - basically
At this all evening and can't see what I'm missing: JS: $(document).ready(function() { $(#form).submit(function
Evening all! I have half a script that I need to change the CSS
Evening all, I just had a quick question about SQL average queries. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.