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

  • Home
  • SEARCH
  • 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 6345879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:54:31+00:00 2026-05-24T20:54:31+00:00

I have a simple Comment model and controller. When I create a comment in

  • 0

I have a simple Comment model and controller. When I create a comment in my application it does not check for the validations I’ve assigned.

This is my comment model:

class Comment < ActiveRecord::Base
  belongs_to :post

  validates_presence_of :commenter
  validates_presence_of :body
end

Here’s my output when creating a comment in the console:

>> comment = Comment.new
=> #<Comment id: nil, commenter: nil, body: nil, post_id: nil, email: nil, created_at: nil, updated_at: nil>
>> comment.save
=> false
>> comment.errors
=> #<OrderedHash {:body=>["can't be blank"], :commenter=>["can't be blank"]}>

Everything looks great.
But if I create a blank comment inside the application it just says that it’s been successfully created and doesn’t actually create a comment.

This is what it logs:

Started POST "/posts/19/comments" for 127.0.0.1 at Tue Aug 16 23:10:26 -0400 2011
Processing by CommentsController#create as HTML
Parameters: {"comment"=>{"body"=>"", "commenter"=>"", "email"=>""}, "commit"=>"Create Comment", "authenticity_token"=>"V/EinZAi2NNYx7AokikTpQFkNtADNiauW5vcNGdhTug=", "utf8"=>"\342\234\223", "post_id"=>"19"}
Post Load (0.1ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = 19 LIMIT 1
Redirected to http://localhost:3000/posts
Completed 302 Found in 23ms

Any thoughts on this? I can add my actual form code as well if it would be of any help.

UPDATE
Controller code:

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment])
    flash[:notice] = "Your comment has been saved."
    redirect_to (:back)
  end
end

UPDATE
View code:

<%= form_for([post, post.comments.build]) do |f| %>
              <div class="field">
                <h4><%= f.label :name %></h4>
                <%= f.text_field :commenter %>
              </div>
              <div class="field">
                <h4><%= f.label :email_address %></h4>
                <%= f.text_field :email %>
              </div>
              <div class="field">
                <h4><%= f.label :body %></h4>
                <%= f.text_area :body %>
              </div>
              <div class="actions">
                <%= f.submit %>&nbsp;
                <%= link_to 'Cancel', nil, :class => 'cancel' %>
              </div>
            <% end %>
  • 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-05-24T20:54:32+00:00Added an answer on May 24, 2026 at 8:54 pm

    You have to manually check if there are errors and display them. This doesn’t happen magically somehow.

    You have to change your controller action like this:

    class CommentsController < ApplicationController
      def create
        @post = Post.find(params[:post_id])
        @comment = @post.comments.build(params[:comment])
    
        if @comment.save
          flash[:notice] = "Your comment has been saved."
          redirect_to (:back)
        else
          render 'new'
        end
      end
    end
    

    You can show the errors in your view like this:

    <%= f.error_messages %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple Rails application that allows people to comment on posts.
I have 3 model, User, Post, Comment with definition like below class Post <
I have an article controller that deals with articles. I have a comment model
ASP.NET MVC 2.0: Simple Model Binding not working/binding as it should i have a
I have a kase model which I am using a simple search form in.
Ok, I have a model that is very simple: ServiceType(id: integer, title: string, duration:
I am creating a simple aspnetmvc cart application and have defined classes similar to
I have a simple Video model in my rails app that has_many comments. I
I have a Blog model that has defined var $hasMany=array(Comment); How can i apply
I have a really simple Rails app. Basically an article with comments. I want

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.