rails 4 can only call javascript once from link_to -
rails 4 can only call javascript once from link_to -
i have rails app list of items wnat delete using javascript , hide row.
what experience if click 1 link, item deletes , row dissapears. if click another, controller not called, pages refreshed through javascript.
show.html.erb
link_to image_tag("bin-20.png", :border => 0), '#', :title => 'remove item', data:{ deleteitem: item.id } item.js.coffee $ -> $("a[data-deleteitem]").click -> urllink = "/itemisations/" + $(this).data("deleteitem") divclass = ".item-" + $(this).data("deleteitem") $.ajax type: "post" url: urllink datatype: "json" data: _method: "delete" $(divclass).toggle() alert("item has been removed list")
if click 1 link , another. output in console is:
post http://local-dev.com:3000/itemisations/636 200 ok 1.6s jquery.js?body=1 (line 9632) http://local-dev.com:3000/items/60# 200 ok 7.88s turboli...?body=1 (line 59) http://local-dev.com:3000/items/60# 200 ok 8.31s
so can see on first click sytem runs delete phone call on controller , shows view. however, on sec click phone call controller not run ...
oh, , reason running link coffee script rather link_to remote straight controller wanted able phone call toggle part of routine
ok, looks though fell foul of turbolink here. around issue changed script following
$(document).on 'ready page:load', -> $("a[data-deleteitem]").click -> urllink = "/itemisations/" + $(this).data("deleteitem") divclass = ".item-" + $(this).data("deleteitem") $.ajax type: "post" url: urllink datatype: "json" data: _method: "delete" $(divclass).toggle() alert("item has been removed list")
javascript ruby-on-rails ruby-on-rails-4 coffeescript
Comments
Post a Comment