Here is my solution for the whole error scene.
I created a part that simply uses the model variable that it would pass when rendering it:
<%# app/views/errors/_error.html.erb %> <%= content_for :message do %> <% if model.errors.any? %> <ul> <% model.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> <% end %> <% end %>
You can easily add dynamic html class names and / or names based on the model name, as well as generic ones.
I have settings where my error messages are displayed in one place in the layout file:
<%# app/views/layouts/application.html.erb %> <%= yield :message %>
If someone did not want this functionality to remove the content_for in partial, this would do the trick.
Then in fact you can simply write:
<%= render 'errors/error', model: @some_model %>
You could extend this even further by creating a batch that takes the collection and uses the error mentioned above:
<%# app/views/errors/_collection.html.erb %> <% collection.each do |model| %> <%= render 'errors/error', model: model %> <% end %>
Cut it off:
<%= render 'errors/collection', collection: @some_model.some_has_many_association %>
I like this way. It is simple, easy to manage / maintain and incredibly adaptable.
Hope this helps!
EDIT: Everything in HAML
-# app/views/errors/_error.html.haml = content_for :message do - if model.errors.any? %ul - model.errors.full_messages.each do |msg| %li= msg
-
= render 'errors/error', model: @some_model
-# app/views/errors/_collection.html.haml - collection.each do |model| = render 'errors/errors', model: @some_model
= render 'errors/_collection', collection: @some_model.some_has_many_association