spanport
spanport

Reputation: 97

AJAX Request from jQuery is missing params in Rails processing

I am working on AJAX requests between jQuery and Rails 3.1 (right now, working on localhost, testing in Chrome and Firefox, if this helps). When a user clicks on a link, I will pass the values of two text fields to the server. Specifically, I have a controller called AnswersController that will store these two values into the "answers" database table.

Below is my front-end code to make the request. The request is successfully received by Rails, as the response function in this request occurs and my paragraph called "last_chance" is filled by what the server responds with.

$('.other_question a').click(function(e) {          
        var this_profile_id = window.location.pathname.substring(1).split('/')[1];
        var this_question_id = window.location.pathname.substring(1).split('/')[3]; // there is a hidden space when splitting

        var ajax_data = {
            type : "PUT", /* should this be POST for older browsers? */
            url : '/profiles/' + this_profile_id + '/answers/' + this_question_id + '.json',
            contentType : 'json',
            dataType : 'json',
            success : function(msg) {
                alert("Data Saved: " + msg );
                $('#last_chance').text(msg['saved']);
            },
            data : {
                _method : 'PUT',
                page : {
                    authenticity_token : $('input[name=authenticity_token]').attr('value'),
                    answer : {
                        response : 'hi there',
                        comments : 'word'
                    },

                    action : 'update',
                    controller : 'answers',
                    profile_id : this_profile_id,
                    id : this_question_id
                }                   
            }               
        };
        $.ajax(ajax_data);
        return false;

    });

Here is my backend code to process this: the method is update in the AnswersController:

def update
    respond_to do |format|
       format.html { redirect_to 'http://google.com'}
       format.json { render :json => {:saved => 'ok: ' + params.to_s }.to_json }
     end

    return
end

Now, when I fill the "last_chance" paragraph element with the params that Rails received, it shows the following:

ok: {"action"=>"update", "controller"=>"answers", "profile_id"=>"11", "id"=>"42", "format"=>"json"}

It seems, unless something has happened that I'm not aware of, that Rails is not accepting or receiving these other parameters that I included in the AJAX call. Could someone explain to me why this is? And how I do I make sure that it receives all the included params?

Thank you very much in advance for any help!

Oh, also: I removed all the data parameters, and the same result was returned (meaning, I guess, that Rails handles the returned params). But how can I pass data then? Where did these custom params go?

EDIT: from a response's suggestion, I changed it to this:

    params[:saved] = 'OK'
    respond_to do |format|
        format.html { redirect_to 'http://google.com'}
        format.json { render :json => params.to_json }
    end

From this, the only output I got in the "last_chance" part was "OK". Where can I go from here?

Upvotes: 1

Views: 1610

Answers (2)

moritz
moritz

Reputation: 25757

The type parameter for the ajax request should be 'POST', you set it specifically for rails backends with the _method data-parameter. If this doesn't fix the problem, can you post your relevant routes? Ah, and dont't set the contentType because the default is fine for your use case.

Upvotes: 1

Vik
Vik

Reputation: 5961

Try to add additional parameters before render , like :

params[:saved] = 'ok: '
format.json { render :json => params.to_json }

Upvotes: 1

Related Questions