How to append to an XML response an error attribute using Ruby on Rails 3?
I am trying to implement REST APIs, so in my RoR3 application I have XML responses. Before to pass to a consumer the XML, I would like to check if there are errors somewhere and, if so, append and send back a response with error messages.
I read "Active Record Validations and Callbacks" guides on the RoR website, but it seems not work in my case.
I extract from the database a resource doing
@response = User.find_by_id(1)
and I would like, if possible, to access @response.errors after a "validation".
Seeing some examples I have seen how to report errors in an XML file
format.xml { render :xml => @response.errors }
but how can I add new errors to the @response?
Maybe something like开发者_如何学JAVA this:
errors.add(:password, "is invalid")
this works too:
errors.add_to_base('your text')
but you should put it in the model.
精彩评论