Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how to do ajax call through jquery function in rails 4 ?

    • 0
    • 4
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 281
    Comment on it

    This is our ruby on rails  view from where called our jquery function(). 

     <td id= <%= "reject_#{event.id}" %>> 
                <%= button_tag "Accept" , :onclick => "abc('#{event.id}', 'Accept');"  %>     
                <%= button_tag "Reject" , :onclick => "abc('#{event.id}', 'Reject');"  %>     
             </td>

    This is our jquery function() from where we called ajax().

    abc = function(id1 , user_status){
         console.log(user_status);
         console.log(id1);
         var url; 
         if(user_status == 'accept'){
              url = "/events/" + id1 + "/accept_event/";
         }
         else {
             url = "/events/" + id1 + "/reject_event/";
         }
         console.log(url);
         $.ajax({ 
            url: url,
            beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
            type: "POST",
               dataType: "JSON",
                data: {'event_id': id1 },
            success: function(data){
              $("#success1").html("ACCEPTED");
              var a = 'reject_' + id1;
               $("#"+a).html(user_status);
            },
             error: function(error){
                alert(error.responseText)
             }
    
          });

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: