15

I am trying to send a jQuery Ajax PUT request that looks like this:

$.ajax({
      type: "PUT",
      url: '/admin/pages/1.json',
      data: { page : {...} },
      dataType: 'json',
      success: function(msg) {
        alert( "Data Saved: " + msg );
      }
});

My controller looks roughly like this:

      respond_to do |format|
         if @page.update_attributes params[:page]
           format.html{ ... }
           format.json{ render :json => {:saved => 'ok'}.to_json }
         else
           format.html{ ... }
           format.json{ render :json => {:saved => 'fail'}.to_json }
         end
       end

but I get the following error.

The error occurred while evaluating nil.name /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/xml_mini/rexml.rb:29:in merge_element!' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/xml_mini/rexml.rb:18:inparse' (DELEGATION):2:in __send__' (__DELEGATION__):2:inparse' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/hash/conversions.rb:154:in `from_xml' ... ...

It is like Ruby on Rails is trying to parse the parameters as XML, but I want to use JSON!

What shall I do to put JSON to Ruby on Rails?

1

5 Answers 5

7

Setting the contentType didn't work for me. I was getting a string instead of a hash for params[:page].

So I solved it like this:

Stringifying the JSON object with a script found at JSON in JavaScript:

$.ajax({
    type: "PUT",
    url: '/admin/pages/1.json',
    data: { page : JSON.strigify( {...} ) },
    dataType: 'json',
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
});

On the Ruby on Rails side, a JSON gem must be required. In the controller:

 params[:page] = JSON.parse params[:page] if params[:page].is_a? String

It is not pretty at all, but it worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Be careful with this - PUT isn't supported by all browsers. Some only do GET and POST, I'm not sure which.
Parsing will be done automatically by Rails if you set the contentType. See my response
5

You need to set contentType in your options. contentType is what you are sending. dataType is what you expect back. You should carefully read the documentation on the options argument to ajax.

1 Comment

data also needs to be serialized. See my answer
2

If you don't want to add scripts, you could do

$.parseJSON("some_jsonish_string")

Comments

2

Also, head's up, but there's a bug in Ruby on Rails 2.3.2 that prevents this from working. See Ruby on Rails 2.3 JSON "put" request routing is broken

Comments

1

You need to set contentType to "application/json" in your options; dataType is only what you expect back.

Also, you need to serialize your data field using JSON.stringify:

$.ajax({
    type: "PUT",
    url: '/admin/pages/1.json',
    data: JSON.stringify({ page :  {...} }),
    contentType: 'application/json',
    dataType: 'json',
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.