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.
The mystery seems to be either (a) you might not be calling the right method, or (b) the
.errorshash doesn't contain what you think it contains.Wrong method?
In your controller you're calling
@app.destroy, but the method that adds the errors is calleddestroy_on_riakAre you sure you don't mean to type this?
Or is there a
before_destroycallback missing from your code sample that in turn callsdestroy_on_riak? From the code included I don't see anywhere that thedestroy_on_riakmethod ever gets called, so this is just a guess.Unexpected contents of
.errorshash?If that's not the problem, then when
@app.errors.any?is returningtrue, then at that point in the code print the contents of@app.errorsto your log so you can see what's wrong.