2

I have two callback functions side by side, and they seem to execute the exact same thing. However, the first one works, and the second one doesn't - it doesn't even execute the javascript, though the file IS rendered.

Here's the function that does not work - in a partial:

<%= button_to 'Delete', todo_path(@item.id, :format => :js, ), 
                :method => :delete,
                :remote => true,
                :form => { "data-type" => "js" },
                :form_class => "btn btn-danger btn-xs" %>

In the controller:

 def destroy
    @item_id = params[:id]
    if item = Item.find_by_id(@item_id)
      item.destroy
    end

    respond_to do |format|
      format.js {}
    end
  end

On the callback (in todos/destroy.js.erb):

alert("foobaz");
console.log("baz-foo");
$("#item_<%= @item_id %>").remove();

The method and the partial are rendered, but the javascript does not execute (no alert, no console log, and no delete of the item):

Started DELETE "/todos/73.js" for ::1 at 2015-04-20 20:24:37 -0700
Processing by TodosController#destroy as JS
  Parameters: {"authenticity_token"=>"sKfPWYP4owepOSDKBtTAoUMN4cVez/d5rz7k+UV0BY9Eu3Qkv/loiSpg4E6hN8Jmqih0+1v0WUROjL8h8IBnyQ==", "id"=>"73"}
  Item Load (4.5ms)  SELECT  `items`.* FROM `items` WHERE `items`.`id` = 73 LIMIT 1
  Rendered todos/destroy.js.erb (0.4ms)

Thoughts?

UPDATE

I just added some Ruby debugging in:

<% print "HERE I AM" %>

And it printed out. So the template is being rendered, and it's just the javascript is not being called. Still not a solution :(

Additionally, in every way I can tell, it looks just like the other function, other than it's being rendered in a partial.

5
  • Can you create a jsFiddle or atleast provide us the HTML and JavaScript instead of Ruby Commented Apr 21, 2015 at 5:32
  • Hi-- The javascript is the third block. It's not being executed at all. Commented Apr 21, 2015 at 5:40
  • Get rid of your respond_to block in your controller.. in your case, it's doing nothing (and I believe the {}) is swallowing your response Commented Apr 21, 2015 at 13:02
  • The {} is the way that Rails executes the default template with nothing else - here's a blog post that describes it - sts10.github.io/blog/2014/04/12/data-remote-true Commented Apr 21, 2015 at 14:39
  • 1
    Try debugging according to this? - alfajango.com/blog/rails-js-erb-remote-response-not-executing Commented Apr 21, 2015 at 17:21

1 Answer 1

2

Thank you very much to fylooi for the suggestion on debugging! It turns out the javascript WAS executing, but just referencing the wrong element on the page. Highly recommended to use Firebug (not the built in debuggers). Thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.