I am teaching myself rails and I would like to set up a basic ajax call where a user can click a div and send an object to the database as well as have that data returned as a JSON object to a page such as /hellos
where this data can then be grabbed via the success/done methods of the JQuery AJAX object.
I have my Javascript setup below and now I need to learn how to capture this with a controller and send it off. This is where I need help. From the Ruby tutorials I've read online they do a good job of explaining basic Rails-style rest development where you modify the controllers to have index,create,new,show,update, edit and destroy as well as how to do Ajax the "rails way" but there isn't much in the way of how to use it the way I'm interested in, mainly using Rails as just a stack for a thick client app where I do this work in Vanilla JavaScript ( and JQuery ).
I currently have two controllers setup. One called home and the other called dashboard. The code below would be sent via a div click on the dashboard.index.erb page. I am trying to evade using the Rails view templating in place of just using JQuery to update the Dom. I will eventually learn this the "rails way" but at the moment I want to start from my present point of reference and could use some direction.
EDIT: I found this excellent tutorial which is exactly what I was looking for just in case anyone has the same question.
$(function() {
var helloWorld = {
hi: "Hello world"
};
//____BEGIN AJAX
function doStuff() {
$.ajax({
type: 'POST',
data: JSON.stringify(
helloWorld
),
contentType: 'application/json',
url: '/hellos',
success: function(datastuff) {
}
});
};
$('.send_this_puppy').on("click", function() {
doStuff();
});
});