This project is archived and is in readonly mode.
2.3.2 ActiveResource JSON doesn't send parameters with root node
Reported by Matthew Moore | April 30th, 2009 @ 06:40 PM | in 2.x
If you create a new 2.3.2 rails project with an ActiveResource model with format set to JSON, e.g.
class MyModel < ActiveResource::Base
self.site = "http://localhost:3001"
self.format = :json
self.timeout = 30
end
When you try to create a my_model, it won't send in the parameters correctly (it will be missing the root node):
> m = MyModel(:foo => 'bar')
> m.save
The parameters sent to the server should be
my_model[foo]=bar
Instead, it's just:
foo=bar
On the other hand, if you set the format to :xml, you will get the correct parameters sent,
my_model[foo]=bar
Comments and changes to this ticket
-
Matthew Moore April 30th, 2009 @ 06:47 PM
Here's the applicable code within ActiveResource::Base, which makes it pretty obvious what's going on:
# Returns the serialized string representation of the resource in the configured # serialization format specified in ActiveResource::Base.format. The options # applicable depend on the configured encoding format. def encode(options={}) case self.class.format when ActiveResource::Formats[:xml] self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) else self.class.format.encode(attributes, options) end end
-
Matthew Moore April 30th, 2009 @ 06:49 PM
- Assigned user set to Rick
-
Joe Martinez June 30th, 2009 @ 08:10 PM
You can fix this by adding
ActiveRecord::Base.include_root_in_json = truein a initializer. Hope this will be default in Rails3
-
Joe Martinez June 30th, 2009 @ 08:11 PM
Nevermind the above - it's for ActiveRECORD. This problem does exist in ActiveResource.
-
Joe Martinez June 30th, 2009 @ 09:05 PM
Patched it.
Added an ActiveResource::Base option to include_root_in_json just like ActiveRecord.
-
Matthew Parker August 27th, 2009 @ 09:58 PM
thanks for this patch joe. i'm working with a big team for migrating a high-traffic website from cold fusion to ruby on rails, and for the first phase, we'll be doing a rails front end / cf backend, with RESTful communication between the two over JSON. Just today i was playing around with ActiveResource over :json and ran into this issue.
any idea on when this might make it into a rails release?
-
Joe Martinez August 27th, 2009 @ 10:23 PM
No problem, I wrote this patch for a client and it's part of a production application. We haven't had any problems with it so far, but I'm sure the Rails 3 guys have lots in mind for the ActiveResource stuff which is why this has probably been looked over.
I actually have this patch in the form of a rails initializer/monkey patch as well. If you need that just let me know.
-
Matthew Parker August 28th, 2009 @ 02:02 PM
Thanks Joe, would you send over your monkey patch? either post it here or email me at moonmaster9000 at gee mail
We also had an issue where rails 2.3.3, by default, returns json with a root node (caused by setting ActiveRecord::Base.include_root_in_json = true in config/initializers/new_rails_defaults.rb). ActiveResource over :json (unlike ActiveResource over :xml) expects a json response without a root node, and doesn't have any logic to handle the situation where the ActiveResource attributes are encapsulated in the root node in the json response.
It's, of course, not the biggest deal (we just set ActiveRecord::Base.include_root_in_json = false in the app sending json representations of ActiveRecord objects), but it is a bit odd now that we have one app sending ActiveRecord responses as json without a root node, and the other app sending ActiveResource :json CREATE/PUT messages with a root node.
-
Jarred Nicholls September 25th, 2009 @ 03:05 AM
I've edited the json_format.rb file to detect and load the json response appropriately when the model name is the root of the response object. Pretty simple solution, but I hope an "elegant & universal" solution makes it to the core so I don't have to remember to keep patching my ActiveResource installations everywhere :-)
-
Elad Meidar September 25th, 2009 @ 07:03 AM
+1 verified on 2-3-stable, but patch failed to apply, here's a patch for 2-3-stable.
-1 on existing patch, fails on 2-3-stable and on master, on master it fails with:
error: activeresource/test/base_test.rb: No such file or directory
This is something i have been patching myself for a while too, definitely a requirement as far as i concern
-
Mike (at coverallcrew) March 4th, 2010 @ 07:10 AM
Anyone know if this patch will be applied to Rails 3?
-
Repository April 5th, 2010 @ 09:49 PM
- State changed from new to committed
(from [72f89b5d971b48a133c4c0af56fbeda35d738dae]) create option to include_root_in_json for ActiveResource [#2584 state:committed] http://github.com/rails/rails/commit/72f89b5d971b48a133c4c0af56fbed...
-
Santiago Pastorino April 5th, 2010 @ 11:07 PM
- State changed from committed to open
This patch still need to be applied on 2-3-stable
-
Santiago Pastorino April 26th, 2010 @ 04:32 AM
- Tag changed from activeresource, bug to activeresource, bug, patch
- Assigned user changed from Rick to Jeremy Kemper
-
Repository April 26th, 2010 @ 05:06 AM
- State changed from open to committed
(from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 state:committed]"
This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.
Should reuse Active Model.
[#2584 state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...
-
Repository April 26th, 2010 @ 05:06 AM
- State changed from committed to incomplete
(from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 state:committed]"
This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.
Should reuse Active Model.
[#2584 state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...
-
Repository April 26th, 2010 @ 11:19 PM
- State changed from incomplete to committed
(from [aa401bd75a055a0bb12094128b01c8ce5fc2abe8]) Add ActiveResource::Base.include_root_in_json, like Active Record, to serialize instances as hash of model name -> attributes hash rather than the bare attributes hash. [#2584 state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/aa401bd75a055a0bb12094128b01c8... -
Repository April 26th, 2010 @ 11:19 PM
(from [7cd1d37a51f5f53f8fc1360f886d26cabf12d969]) Reuse Active Model serialization in Active Resource. [#2584 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/7cd1d37a51f5f53f8fc1360f886d26...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Tags
Referenced by
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node (from [72f89b5d971b48a133c4c0af56fbeda35d738dae]) create ...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node (from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert ...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node [#2584 state:incomplete] http://github.com/rails/rails/c...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node (from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert ...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node [#2584 state:incomplete] http://github.com/rails/rails/c...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node (from [aa401bd75a055a0bb12094128b01c8ce5fc2abe8]) Add Act...
- 2584 2.3.2 ActiveResource JSON doesn't send parameters with root node (from [7cd1d37a51f5f53f8fc1360f886d26cabf12d969]) Reuse A...