I'm using the gem Responders but I'm not able to show errors that I create on my models using erros.add(:base, 'Error message')
.
On my controller, before the respond_with @app
, I debugged the @app
object and it has errors @app.errors.any?
returns true
On my view, when I check the flash
and @app
objects, none has the error
App controller
# app_controllers.rb
def destroy
@app = current_company.apps.find(params[:id])
@app.destroy
respond_with @app
end
App model
# app.rb
before_destroy :destroy_on_riak
# ...
def destroy_on_riak
# SOME CODE HERE
rescue Exception => e
errors.add(:base, I18n.t("models.app.could_not_destroy", :message => e.message))
return false
end
App view
# apps.html.haml
-flash.each do |name, msg|
%div{:class => "flash #{name}"}
=content_tag :p, msg if msg.is_a?(String)
This is the @app object before the @app.destroy
"#<ActiveModel::Errors:0x00000004fa0510 @base=#<App id: 34, ...>, @messages={}>"
This is the @app object after the @app.destroy
"#<ActiveModel::Errors:0x00000004fa0510 @base=#<App id: 34, ...>, @messages={:base=>[\"Não foi possível excluir a aplicação: undefined method `get_or_new' for #<App:0x00000004f824c0>\"]}>"
I have removed what's inside the @base=
for simplicity.
I'd have to agree with @p.mastinopoulos on this. This should really be handled with the builtin validations. Sounds like you are in need of building a custom validator.
http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate
Try replacing your
before_destroy
with avalidate
:Haven't tried this, but if it doesn't work, you may want to consider creating a custom Validator as referenced in the docs.