views:

238

answers:

3

Rails 3.0 deprecated f.error_messages and now requires a plugin to work correctly - I however want to learn how to display error messages the (new) native way. I am following the getting started guide, which uses the deprecated method when implementing the comments form. For example:

<h2>Add a comment:</h2>
<%= 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 %>

Here is the correct way to do it (as generated by the scaffold):

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 . . . 

I understand that I use the @post variable in the latter example, but what variable do I reference in the former to get the error messages for comment creation?

+1  A: 

I'm pretty sure all you'd need to do is reference @post.comments

So you could do something like:

<% @post.comments.each do |comment| %>
    <% if comment.errors.any? %>

    <% comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
    <% end %>
<% end %>

Or just pull all the errors out out:

comment_errors = @post.comments.map {|c| c.errors}

and then loop through them in your display logic to output each of the comment errors.

Lukas
Thanks for the attempt, but no, that isn't working. Perhaps a simpler way to ask the question would be "How do I display comment errors without using "f.error_messages" given "[@post, @post.comments.build]" instead of something simple like @post?
Craig
honestly, I've never seen someone do `form_for([@post, @post.comments.build])`. Rather it's always been `form_for(@post) do |p|` and then `p.fields_for(@post.comments.build) do |c|`. So you may look into using fields for inside of the post form instead?
Lukas
A: 

I guess that the [@post, @post.comments.build] array is just passed to polymorphic_path inside form_for. This generates a sub-resource path for comments (like /posts/1/comments in this case). So it looks like your first example uses comments as sub-resources to posts, right?.

So actually the controller that will be called here is the CommentsController. The reason why Lukas' solution doesn't work for you might be that you actually don't use @post.comments.build inside the controller when creating the comment (it doesn't matter that you use it in the view when calling form_for). The CommentsController#create method should look like this (more or less):

def create
  @post = Post.find(params[:post_id]
  @comment = @post.comments.build(params[:comment])

  if(@comment.save)
    // you would probably redirect to @post
  else
    // you would probably render post#show or wherever you have the form
  end
end

Then you can use the code generated by scaffolding, only replace @post instance variable with @comment in all the lines except form_for call.

I think it may also be a good idea to add the @comment = @post.comment.build to the controller method that displays this form and use form_for([@post, @comment], ...) to keep the form contents displayed in the form if there're errors.

If this doesn't work and you're not able to figure it out, please add your CommentsController#create method to the question.

Matt
A: 

The best and clean way to implement error_messages in your form is by implementing error_messages in a FormBuilder.

For example, here is the error_messages method I've implemented for my last project. By implemeting your own FormBuilder you can follow the rules and styles of your webdesigner... Here is an example that will output the errors list in ul/li's with some custom styles :

class StandardBuilder < ActionView::Helpers::FormBuilder
  def error_messages
    return unless object.respond_to?(:errors) && object.errors.any?

    errors_list = ""
    errors_list << @template.content_tag(:span, "There are errors!", :class => "title-error")
    errors_list << object.errors.full_messages.map { |message| @template.content_tag(:li, message) }.join("\n")

    @template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
  end
end

Then in my forms :

= f.error_messages

And that's all.

slainer68
better simplest is use the plugin...
shingara