ActiveResource and validation errors

65 views
Skip to first unread message

lathiat

unread,
Sep 30, 2009, 12:03:00 PM9/30/09
to Ruby on Rails: Talk
Howdy All,

I am trying to implement a fairly simple REST controller which I am
then interfacing with ActiveResource. The problem I am having is when
the validation fails, the errors are not being returned to the model
on the ActiveResource style in a way I can access.

I am running Rails 2.3.4 on both sides of the application. The code I
have in my controller for the 'update' method is as follows:

def update
@credit_card = CreditCard.find(params[:id])

respond_to do |format|
if @credit_card.update_attributes(params[:credit_card])
flash[:notice] = 'CreditCard was successfully updated.'
format.html { redirect_to(@credit_card) }
format.xml { head :ok }
else
logger.debug(@credit_card.errors.to_xml)
format.html { render :action => "edit" }
format.xml { render :xml => @credit_card.errors, :status
=> :unprocessable_entity }
end
end
end

The most notable part is the "format.xml { render :xml =>
@credit_card.errors, :status => :unprocessable_entity }".

I checked with the logger.debug the XML is being generated and looks
reasonable to me. The XML I am seeing is:
<?xml version="1.0" encoding="UTF-8"?>
<errors>
<error>Card expiry year must be greater than or equal to 9</error>
</errors>

I have checked this seems to return OK, what I have noticed is when we
compare to the ActiveResource documentation, there is no "type=array"
evident.

ActiveResource::Base example of an error return:
<errors type="array"><error>First cannot be empty</error></errors>

I also found the following bug report;
http://dev.rubyonrails.org/ticket/8872

Which seems to describe this problem (lack of tyoe=array) but has
stalled and the posted "fix" does not work.


Additionally I tested this by doing a simple return with the example
code;
format.xml { render :xml => '<errors type="array"><error>First cannot
be empty</error></errors>', :status => :unprocessable_entity }


However my object still shows an empty errors array:
--- &amp;id001 !ruby/object:ActiveResource::Errors
base: !ruby/object:CreditCard
attributes:
<cut>
errors: *id001
prefix_options: {}

errors: {}



Was hoping to be pointed to either an example of someone else doing
this successfully or some more in depth information on what I've done
wrong. Either is fine!

Appreciate your time.

Best Regards,
Trent

vanne

unread,
Oct 17, 2009, 2:47:04 PM10/17/09
to Ruby on Rails: Talk
i have the exact same problem!
any solution yet?

vanne

unread,
Oct 17, 2009, 2:53:27 PM10/17/09
to Ruby on Rails: Talk
went back to 2.3.3 from 2.3.4
works without any problems

On Sep 30, 6:03 pm, lathiat <lath...@bur.st> wrote:
Reply all
Reply to author
Forward
0 new messages