2

I've been googling but I can't seem to get the right setup, through the things I've seen on google I ended up with this:

var dataString = $('#markerform').serialize();
        var id = $('#route_id').val()
        $.ajax({
            type: 'POST',
            url:  '/routes/'+id,
            data: { "_method":"put", dataString},
            dataType: "JSON",
            success: function(data) {
                console.log(data);
            }
        });

And this for my routes controller

def update
    @route = Route.find(params[:id])
    respond_to do |format|
        if @route.update_attributes
            format.html
            format.json { render text: "Done" }
        else
            format.json { render text: "Bad" }
        end
    end
end

But I end up with this firebug error:

SyntaxError: invalid object initializer
[Break On This Error]   

data: { "_method":"put", dataString},

How might I clean this up so I can get some success going through this app. Thanks.

Datsik
  • 14,453
  • 14
  • 80
  • 121
  • `type: 'PUT'` should work – jvnill Mar 12 '13 at 06:51
  • @jvnill but I read it's not all browser compatible and I want to make sure I'm compatible the whole way across :)? – Datsik Mar 12 '13 at 06:51
  • not sure about that. but in order to fix your error, you should append the additional data to the serialized attributes. if I remember correctly, dataString will be a string so just add the _method to it like `dataString + '&_method=put'` and just pass it to data like `data: dataString` – jvnill Mar 12 '13 at 07:00
  • @jvnill could you look at my controller and tell me why it's giving me a 500 error on submit? – Datsik Mar 12 '13 at 07:00
  • @jvnill nevermind, fixed it, I was missing (params[:route]) from the update_attributes – Datsik Mar 12 '13 at 07:04
  • 1
    ah cool. so what fixed your question? – jvnill Mar 12 '13 at 08:53

3 Answers3

1

Try this (※Use serializeArray instead of serialize):

    $.ajax({
        type: 'PUT',
        url:  '/routes/'+$('#route_id').val(),
        data: $('#markerform').serializeArray(),
        dataType: "JSON",
        success: function(data) {
            console.log(data);
        }
    });
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

Try this

var dataString = $('#markerform').serialize();
        var id = $('#route_id').val()
        $.ajax({
            type: 'PUT',
            url:  '/routes/'+id,
            data: dataString,
            dataType: "JSON",
            success: function(data) {
                console.log(data);
            }
        });
K D
  • 5,889
  • 1
  • 23
  • 35
  • but I read it's not all browser compatible and I want to make sure I'm compatible the whole way across :)? – Datsik Mar 12 '13 at 06:53
  • The PUT method comes in XHTML2.0 version. however you can pass it via $.ajax as it will just go as a parameter and your server will handle method type PUT like now. – K D Mar 12 '13 at 07:29
  • But I'm receiving the error listed at the bottom of my post, I think I have the wrong formatting of the data: any idea what's causing it? – Datsik Mar 12 '13 at 07:33
  • As you have mentioned Type, please remove "_method":"put", and try again. – K D Mar 12 '13 at 08:17
-1

It's been answered over at How do I PUT data to Rails using JQuery. Basically, Rails lets you do PUT calls (& any others that are unsupported) by doing a POST but adding a "_method" parameter with a value of "PUT".

Community
  • 1
  • 1