In my Rails 4.1 app I'm using JQuery-ujs to update cells in a table in my comments#index
view. So in my comments.js.coffee
file I have
jQuery ->
$('tbody').on 'click', '.btn-info', ->
$(this).closest('tr').children('td').eq(4).attr("id", "target")
and the corresponding update.js.erb
file
$('#target').html("<%=j @comment.state.humanize %>").removeAttr("id")
The view renders different groups of comments into the table via custom routes of the form /comments/pending
, /comments/confirmed
etc. and all goes well so long as I request the view directly in the browser address bar.
But I also have breadcrumb links on the page which are generated via link_to
helpers:
<ol class="breadcrumb">
<li class="active">
<a href="/comments/pending">pending</a>
</li>
<li>
<a href="/comments/confirmed">confirmed</a>
</li>
<li>
<a href="/comments/notable">notable</a>
</li>
<li>
<a href="/comments/eminent">eminent</a>
</li>
</ol>
When I click on any of these links to get the view, the page is correctly loaded, but inspection shows that no click handler is attached to the <tbody>
element. (It is attached if I reload the page however.)
How can I ensure that the click handler is always attached?