Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

2.3.2 ActiveResource JSON doesn't send parameters with root node

#2584

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

Reported by Matthew Moore · April 30th, 2009 @ 06:40 PM

State: committed
Milestone: 2.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Matthew Moore
    Matthew Moore

    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
    

    April 30th, 2009 @ 06:47 PM

  2. Matthew Moore
    Matthew Moore
    • Assigned user set to Rick

    April 30th, 2009 @ 06:49 PM

  3. Joe Martinez
    Joe Martinez

    You can fix this by adding
    ActiveRecord::Base.include_root_in_json = true

    in a initializer. Hope this will be default in Rails3

    June 30th, 2009 @ 08:10 PM

  4. Joe Martinez
    Joe Martinez

    Nevermind the above - it's for ActiveRECORD. This problem does exist in ActiveResource.

    June 30th, 2009 @ 08:11 PM

  5. Joe Martinez
    Joe Martinez

    Patched it.

    Added an ActiveResource::Base option to include_root_in_json just like ActiveRecord.

    June 30th, 2009 @ 09:05 PM

  6. samvincent
    samvincent

    Looked over Joe's patch. Looks clean.

    July 2nd, 2009 @ 05:50 PM

  7. Matthew Parker
    Matthew Parker

    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?

    August 27th, 2009 @ 09:58 PM

  8. Joe Martinez
    Joe Martinez

    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.

    August 27th, 2009 @ 10:23 PM

  9. Matthew Parker
    Matthew Parker

    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.

    August 28th, 2009 @ 02:02 PM

  10. Jarred Nicholls
    Jarred Nicholls

    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 :-)

    September 25th, 2009 @ 03:05 AM

  11. Elad Meidar
    Elad Meidar

    +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

    September 25th, 2009 @ 07:03 AM

  12. Mike (at coverallcrew)
    Mike (at coverallcrew)

    Anyone know if this patch will be applied to Rails 3?

    March 4th, 2010 @ 07:10 AM

  13. Santiago Pastorino
  14. Repository
  15. Santiago Pastorino
    Santiago Pastorino
    • State changed from committed to open

    This patch still need to be applied on 2-3-stable

    April 5th, 2010 @ 11:07 PM

  16. Santiago Pastorino
    Santiago Pastorino
    • Tag changed from activeresource, bug to activeresource, bug, patch
    • Assigned user changed from Rick to Jeremy Kemper

    April 26th, 2010 @ 04:32 AM

  17. Repository
    Repository
    • State changed from open to committed

    (from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]"

    This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.

    Should reuse Active Model.

    [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...

    April 26th, 2010 @ 05:06 AM

  18. Repository
    Repository
    • State changed from committed to incomplete

    (from [2a6e0f34ad6b48dcf41989e0d7555cda46492b34]) Revert "create option to include_root_in_json for ActiveResource [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:committed]"

    This reverts commits 72f89b5d971b48a133c4c0af56fbeda35d738dae, 137d8e0b2fe9fcc4fdac6cbbd44ca010784e5972.

    Should reuse Active Model.

    [#2584 2.3.2 ActiveResource JSON doesn't send parameters with root node state:incomplete] http://github.com/rails/rails/commit/2a6e0f34ad6b48dcf41989e0d7555c...

    April 26th, 2010 @ 05:06 AM

  19. Repository
    Repository
    • 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 2.3.2 ActiveResource JSON doesn't send parameters with root node 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...

    April 26th, 2010 @ 11:19 PM

  20. Repository