I’ve been trying to set up a new Rails 3 project as per
Getting Started with Rails — Ruby on Rails Guides, but I’m running
into difficulty with displaying errors when adding a new comment. The
comments are a nested resource of posts. On the posts/show page, I
have
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= f.error_messages %>
<%= f.text_field :commenter %>
<%= f.text_area :body %>
But the f.error_messages is deprecated. I’ve read online that one
solution is to use a partial and pass in the offending object as a
target to it, but I don’t know how to reference the comments object.
It would look something like
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= render"shared/error_messages", :target => @comment %>
<% end %>
where @comment is the comment object. The examples online don’t show
how to do it when you pass an array to form_for. How can I get the
error messages to show up for new comments?
For reference, the CommentsController looks like:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
end
and the Comment model looks like:
class Comment < ActiveRecord::Base
validates :commenter, :presence => true
validates :body, :presence => true
belongs_to :post
end