Tony Beninate
Tony Beninate

Reputation: 1985

validation problem

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

Upvotes: 2

Views: 122

Answers (1)

Mischa
Mischa

Reputation: 43298

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

Upvotes: 2

Related Questions