views:

44

answers:

1

I am using jQuery with rails. I am trying to make a PUT request to update a task. The call is made successfully. However, the javascript code returned does not execute. The code is returned alright but it just does not execute.

Here is the jQuery put request i am making.

$("#droppable_<%= bin.id -%>").droppable({
      drop: function(event, ui) {
        div_ids = ui.draggable.attr('id').split('_');
        $.post('/tasks/' + div_ids[div_ids.length - 1], {'task[bin_id]' : '<%= bin.id -%>', _method:'PUT'}, function(data) {

        });
      }
    });

This is the rjs code.

render :update do |page|
    page.alert 'success'
    page.remove "draggable_#{params[:id]}" if old_bin_id.to_s != params[:task][:bin_id]
    page.replace_html "count_#{old_bin_id}", current_user.tasks.bin(old_bin_id).size
    page.replace_html "count_#{params[:task][:bin_id]}", current_user.tasks.bin(params[:task][:bin_id]).size
end

This is what I get on completion of the request.

try {alert("success");jQuery("#draggable_2").remove();jQuery("#count_2").html("");jQuery("#count_3").html("2");} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('alert(\"success\");\njQuery(\"#draggable_2\").remove();\njQuery(\"#count_2\").html(\"\");\njQuery(\"#count_3\").html(\"2\");'); throw e }

Which is correct but it just does not execute. Any idea why?

A: 

Because if you want to handle rjs using jQuery there are two way to do that:

$("#droppable_<%= bin.id -%>").droppable({
  drop: function(event, ui) {
    div_ids = ui.draggable.attr('id').split('_');
    $.post('/tasks/' + div_ids[div_ids.length - 1], {'task[bin_id]' : '<%= bin.id -%>', _method:'PUT'}, function(data) {
       eval(data) #run js ajax returned
    });
  }
});

or

$("#droppable_<%= bin.id -%>").droppable({
  drop: function(event, ui) {
    div_ids = ui.draggable.attr('id').split('_');
    $.ajax({
         url:'/tasks/' + div_ids[div_ids.length - 1],
         data: {'task[bin_id]' : '<%= bin.id -%>', _method:'PUT'},
         dataType: "script",
         type: "POST"
    });
  }
});
allenwei
`eval(data)` worked for me. Thanks!
Chirantan