Responders gem: add a model's base errors to the flash message?

293 Views Asked by At

Is there an easy way to add a model's base errors to the flash message in the responders gem?

When I try to delete a record with depending children that has dependent: :restrict_with_error set, then I see an error like "X could not be destroyed", but nothing more.

Inspecting the record, I see that there is an additional error added to base:

@messages={:base=>["Cannot delete record because dependent children exist"]}, @details={:base=>[{:error=>:"restrict_dependent_destroy.has_many", :record=>"children"}]

Is there an easy way to append base errors to the flash message?

1

There are 1 best solutions below

1
On

You use following code in order to display flash errors message

if object.destroy
    flash[:success] = "Success Message"
elsif object.errors.messages[:base].present?
    flash[:error] = object.errors.messages[:base]
else
    flash[:error] = 'Object Not Destroyed'
end

and write following code on view to display flash message

<% flash.each do |key, value| %>
  <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>