Joshua Muheim
Joshua Muheim

Reputation: 13195

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

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?

Upvotes: 4

Views: 301

Answers (1)

Ganesh
Ganesh

Reputation: 2004

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 %>

Upvotes: 0

Related Questions