4

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?

weew
  • 363
  • 3
  • 12
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152

1 Answers1

0

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 %>
Ganesh
  • 1,924
  • 1
  • 18
  • 31
  • 1
    I'm sorry, I should have stated more clearly that I'm asking for a way to do this automatically using the FlashResponder (responders gem). – Joshua Muheim Jan 29 '18 at 17:16