1

I have a comment system and you will be able to reply to each comment. I have a partial for the reply form. How can I make it so that when you click reply on the comment, the partial shows up underneath the comment so no refreshing or going to a new page is needed. I'm assuming ajax but I'm not sure how it'd work.

Jill
  • 533
  • 7
  • 22

2 Answers2

2
(function($){
    function processForm( e ){
        $.ajax({
            url: 'YOUR_BACKEND_URL',
            dataType: 'text',
            type: 'post',
            contentType: 'application/x-www-form-urlencoded',
            data: $(this).serialize(),
            success: function( data ){
                $('#response pre').html( data );
            },
            error: function( errorThrown ){
                console.log( errorThrown );
            }
        });

        e.preventDefault();
    }

    $('#my-form').submit( processForm );
})(jQuery);

backend_url could be : processComments.rb Now your page wouldn't need to refresh when someone makes a comment, you will get the data the user entered in the success callback and use jQuery to display it on the page. Also you would have to save the data in the processComments.rb Ajax tuts

Walk through to using ajax with ruby

Community
  • 1
  • 1
  • Thanks. This helped also http://railscasts.com/episodes/136-jquery-ajax-revised?autoplay=true – Jill Aug 20 '15 at 00:27
1

Check out pjax: https://github.com/defunkt/jquery-pjax

The obligatory Railscasts link: http://railscasts.com/episodes/294-playing-with-pjax

Karl Wilbur
  • 5,898
  • 3
  • 44
  • 54
  • So will pjax work even though the url won't change. All that's changing is that I'm adding a form to the page. Nothing should be removed. – Jill Aug 19 '15 at 23:50