This is how I solved it. (Note that in the future I will obviously just include only the most relevant lines.)
A model can have multiple checks and even methods that can report multiple errors.
class Order < ActiveRecord::Base validates :name, :phone, :email, :presence => true def some_method(arg) errors.add(:base, "An error message.") errors.add(:base, "Another error message.") end end
In addition, the action of the controller can set flash messages. Finally, the user can enter data in the input fields, and we want him to continue through redirect_to .
class OrdersController < ApplicationController def create @order = Order.new(params[:order]) respond_to do |format| if @order.save session.delete(:order)
Depending on your installation, you may or may not need to save model data, such as an order, per session. I did this to transfer the data back to the original controller and thereby was able to adjust the order again.
In any case, in order to display the actual error and flash messages, I did the following (in views/shared/_flash_messages.html.erb , but you could do this in application.html.erb or wherever it makes sense for your application ) And this is thanks to this line flash[:error] = @order.errors.to_a
<div id="flash_messages"> <% flash.each do |key, value| # examples of value: # Woohoo notice! # ["The server is on fire."] # ["An error message.", "Another error message."] # ["Name can't be blank", "Phone can't be blank", "Email can't be blank"] if value.class == String # regular flash notices, alerts, etc. will be strings value = [value] end value.each do |value| %> <%= content_tag(:p, value, :class => "flash #{key}") unless value.empty? %> <% end %> <% end %> </div>
To be clear, regular flash messages, such as notifications, warnings, etc., will be strings, however errors will be arrays since the aforementioned call was errors.to_a