hlim
hlim

Reputation: 945

Send JSON Object to Rails

Hi I'm new to rails so please bear with me :) I want to send JSON object to rails and save it to database. basically I have a data entered by the user then I have a javascript function thats retrieves the data and convert it into JSON object. What i want is to sent the JSON object maybe through ajax to rails and save it to the database.Can anyone give me any idea how to do it. thnx

function save(){
----(some codes)   
notesArray.push({ Index: i, Title: title.val(), Content: content.val()});

// json encode it
var jsonStr = JSON.stringify(notesArray);

//want to add code here to send to rails
}

Upvotes: 0

Views: 1227

Answers (2)

Chaoyu
Chaoyu

Reputation: 842

As to "handle the JSON object in the controllers" you can use parsed_params = CGI.parse(params[:_json]) ,then you can get what you what througt parsed_params[key1][key2]

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359776

If you're using jQuery, it's very easy to do this using $.post():

$.post('/some/page/here', {notes: jsonStr});

Upvotes: 1

Related Questions