1
votes

I'm curious how Rails 5 renders partials and if there's a gotcha that I'm not aware of. I have two partials on my page, one that is rendered as part of the html and the other that renders inside the success callback of an ajax request. However, when I refresh the page, the partial inside the ajax request is actually being rendered even when the ajax request is not fired, and EVEN when the call to render the partial is commented out.

I had always assumed that partials render at the time the line of code is run.

Here is the relevant code:

$('body').on("click", ".notif-popup-container", function() {

  $.post(
    "/notifications/read",
    { notif_id: notifId },
  ).done(function(data) {
    $('#notifUnreadCount').html(data.unread_notifs);
    
    var popover = $('#notificationsBell').data('bs.popover');
    popover.config.content = $('.notifications-list').html();
  })


  $('.notifications-list').html("<%= j (render partial: 'notifications/partials/test_partial') %>"); // this never fails to render, even when commented out

})
<div style="display:none" class="notifications-list">
  <%= render 'notifications/partials/popover_notifications' %>
</div>

Here is the console output image. As you can see _popover_notifications.html.erb renders as it should, but then my _test_partial.html.erb also renders, which it shouldn't, since the click handler it's in was not fired.

rails server output

Any insight appreciated.

1
Could you please grep 'test_partial' across the project, maybe you render it somewhere else? Maybe inside 'popover_notifications.html.erb '?Vasilisa
Hi @Vasilisa that's the only place I have it unfortunatelyKevin Sun

1 Answers

1
votes

I had always assumed that partials render at the time the line of code is run.

The key here is understanding when the code is run. When you dynamically create javascript with js.erb the file is first run through ERB on the server before it is sent to the client which runs the resulting javascript. Your server does not actually parse the javascript so it does not know or care that there is a event handler involved. To Rails its just a string buffer.

This is actually a major conceptual problem with js.erb templates as the context switching between server and client side makes the flow hard to follow.

There are also numerous other problems with this code like the use of the === identity operator and the fact that your performing DB queries in the view which is a huge anti-pattern.