Creating Ruby-on-Rails Flash Errors

In this example we will be creating simple flash errors for an application that allows users to create new heroines. If a parameter for heroine creation is not met, a flash error is displayed.

In the controller (under create, or whatever form you want the error messages to appear on):

if @heroine.valid?
redirect_to @heroine
elseflash[:error] = @heroine.errors.full_messagesredirect_to new_heroine_pathend

Implemented example:

def create
@heroine = Heroine.create(heroine_params)
if @heroine.valid?redirect_to @heroineelseflash[:error] = @heroine.errors.full_messagesredirect_to new_heroine_pathendend

In your ERB file (new or edit, wherever you want the error messages to appear on) add the flash errors:

<% if flash[:error] %><% flash[:error].each do |e| %><%= e %><% end %><% end %>

Developer with a business background