rails/rails — Issue #3268: ActiveResource generates wrong XML when no attributes are set
Author: NoICE
Created: 2011-10-09 13:37:06+00:00
State: open

Hi, I came around this issue just now.

I have in client app AR model like this:

``` ruby
class Cm::Addresslist < ActiveResource::Base
self.site = "http://localhost:3000"
self.prefix = "/api/"
self.format = ActiveResource::Formats::XmlFormat
end
```

Launching Rails console and typing this:

```ruby
ree-1.8.7-2011.03 :066 > a = Cm::Addresslist.new
=> #
ree-1.8.7-2011.03 :067 > a.save
ActiveResource::ServerError: Failed. Response code = 500. Response message = Internal Server Error.
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/connection.rb:148:in `handle_response'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/connection.rb:115:in `request'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/connection.rb:98:in `post'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/connection.rb:218:in `with_auth'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/connection.rb:98:in `post'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/base.rb:1353:in `create_without_notifications'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/observing.rb:19:in `create'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/base.rb:1148:in `save_without_validation'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/validations.rb:79:in `save_without_notifications'
from /Users/daliborfilus/.rvm/gems/ree-1.8.7-2011.03/gems/activeresource-3.1.1/lib/active_resource/observing.rb:19:in `save'
from (irb):67
ree-1.8.7-2011.03 :068 >
```

From the service log I can see this:

```
Started POST "/api/addresslists.xml" for 127.0.0.1 at Sun Oct 09 15:33:12 +0200 2011
Processing by Api::AddresslistsController#create as XML
Parameters: {"addresslist"=>"\n"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'noice@email.cz' LIMIT 1
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 1 LIMIT 1
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 286ms

NoMethodError (undefined method `stringify_keys' for "\n":String):
app/controllers/api/addresslists_controller.rb:37:in `create'
```

Notice the " {"addresslist"=>"\n"}" in there.
When I supply at least one parameter before save, it passes.

client:

```ruby
ree-1.8.7-2011.03 :068 > a.name = "test"
=> "test"
ree-1.8.7-2011.03 :069 > a.save
=> true
```

server:

```
Started POST "/api/addresslists.xml" for 127.0.0.1 at Sun Oct 09 15:35:02 +0200 2011
Processing by Api::AddresslistsController#create as XML
Parameters: {"addresslist"=>{"name"=>"test"}}
```

Using Rails 3.1.1 (both server and client)
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin11.0.0], MBARI 0x6770, Ruby Enterprise Edition 2011.03

Don't know if this is AResource issue or if its Rack issue, but it's a bug for sure.