f.error_messages in Rails 3.0

前端 未结 6 1022
难免孤独
难免孤独 2020-11-29 00:04

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.

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 00:23

    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| %>
            
  • <%= msg %>
  • <% end %> <% end %> <% end %>

    Or just pull all the errors out out:

    comment_errors = @post.comments.map(&:errors)
    

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

提交回复
热议问题