0

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


    });
});
William
  • 4,422
  • 17
  • 55
  • 108
  • 1) **I would like to set up a basic ajax call where a user can click a div and send an object to the database** Just be aware that you can only send Strings back and forth. The Strings then have to be converted to objects. 2) Print statements are almost always the easiest way to debug programs. In js, the print statement is either `alert()` or `console.log(`); and in a rails action, `puts()` will output text in the server window. In the server window, it can be hard to see your output mixed in with the server's output, so I often do `puts "********#{something}"`. – 7stud Oct 05 '14 at 20:28
  • 3) As I think you've discovered, inside a rails action the params Hash is your means for grabbing data sent in a request. 4) I think you are approaching things the right way. The rails ajax way is a pretty confusing--especially if you are not experienced with js and know what executes in the browser and what executes on the server side. I just posted some info in another answer about how `remote: true` works, which you may or may not find useful: http://stackoverflow.com/questions/26183781/rails-4-how-to-update-index-page-with-ajax/26189488#26189488 – 7stud Oct 05 '14 at 20:36
  • 5) The `data` setting in the `ajax()` function can be a js object, so you can just write: `data: helloWorld`, or simply: `data: {hi: "Hello world"}`. See the jQuery docs here: http://api.jquery.com/jquery.ajax/ – 7stud Oct 05 '14 at 20:42
  • Shouldn't your `console.log("yay")` go inside your success function, and wouldn't you want to `console.log` the returned data instead, so you can see what came back to verify that it's what you expected? – Jason Swett Oct 06 '14 at 18:35
  • Sorry about that, that shouldn't have been there. It was just left over cruft prior to adding the code – William Oct 06 '14 at 19:42

0 Answers0