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.

6
  • type: 'PUT' should work Commented Mar 12, 2013 at 6:51
  • @jvnill but I read it's not all browser compatible and I want to make sure I'm compatible the whole way across :)? Commented Mar 12, 2013 at 6: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 Commented Mar 12, 2013 at 7:00
  • @jvnill could you look at my controller and tell me why it's giving me a 500 error on submit? Commented Mar 12, 2013 at 7:00
  • 1
    ah cool. so what fixed your question? Commented Mar 12, 2013 at 8:53

3 Answers 3

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);
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

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);
            }
        });

4 Comments

but I read it's not all browser compatible and I want to make sure I'm compatible the whole way across :)?
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.
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?
As you have mentioned Type, please remove "_method":"put", and try again.
-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".

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.