Frage

I'm trying to do an Ajax call using goog.net.XhrIo from the Closure library. I want to send a few parameters in a Post request and make them available to a Rails app. Here's my Ajax call:

goog.require('goog.net.XhrIo');
goog.require("goog.structs.Map");
goog.require("goog.Uri.QueryData");

function doRequest() {

    var request = new goog.net.XhrIo();

    var qd = goog.Uri.QueryData.createFromMap(new goog.structs.Map({
        title: "Test ajax data",
        content: "foo",
        param1: 70
    }));

    goog.events.listen(request, goog.net.EventType.COMPLETE, function(){
        console.log("Success!");
    });
    request.send('http://localhost:3000/some_method', 'POST', qd.toString());

}

Then in Rails:

def some_method
  @my_variable = ModelName.find(params[????])
end

The problem is I don't know how to access the parameters in the Rails app. I'm not even sure I'm doing the Post request in such a that the parameters are easily accessible.

War es hilfreich?

Lösung

I'm not sure this is the best way to do this, but this ended up working.

JavaScript:

goog.require('goog.net.XhrIo');
var updateQuizStats = function(result) {
    var request = new goog.net.XhrIo();
    goog.events.listen(request, goog.net.EventType.COMPLETE, function(){
        console.log("YES! YES! YES!");
    });
    request.send('http://localhost:3000/some_method/'+param1+'/'+param2);
}

Rails routes.rb:

match '/some_method/:param1/:param2', :to => 'controller_name#some_method'

In the controller:

def some_method
  @my_variable = ModelName.find(params[:param1])
  @my_variable.update_attributes(:some_attribute => params[:param2])
end

Would love to hear if there's a better way to do this.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top