All threads / Hi Chris How do I add jquery and ajax to the comments form from your tutorial?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Hi Chris How do I add jquery and ajax to the comments form from your tutorial?

emmanuel alcime asked in General

I wrote some jquery to accomplish this here is the code.

$(window).load(function(e){

$("#comment_body").on('click',function(){
$(this).removeAttr('placeholder');

});

var comment = $("#comment_body").val();
var id = '#{params[:video_id]}';
$("#comment_body").keypress(function(event) {
if (event.which == 13) {
var request = $.ajax({
type:"POST" ,
url: '/videos/comments' ,
data:{ "comment":$(this).val()},
timeout : 5000

  }); 
  request.success(function() {
          $("#error-information").html("comment has been posted");
        });      
  request.error(function(httpObj, textStatus) {       
            if(httpObj.status==401){
                window.location ="http://localhost:3000/users/sign_in";
             }
        });
  // Clear out the form so it can be used again    
   $('#comment_body').val('').blur();
   $("#comment_body").attr('placeholder', 'Join The discussion...');       
}    

});

});

But I keep getting this error from the comments controller?

NoMethodError in CommentsController#create

undefined method `comments' for nil:NilClass
at this line @comment = @commentable.comments.new comment_params
Now can someone tell me exactly what I am doing wrong here?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.