I've been trying to set up a new Rails 3 project as per
http://guides.rubyonrails.org/getting_started.html, 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 %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
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
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment