Question

I need to run a conditional inside a .js.erb file when an ajax call is made.

I have the following:

function updateContent() {
  $('.organiser__holder').html('<%= escape_javascript render("filter_links") %>');

  $('.houses').html('
    <% if @houses.count > 0 %>
      <%= escape_javascript render(@houses) %>
    <% else %>
      <%= escape_javascript '<div class="message-box"><p>No houses matching those parameters</p></div>' %>
    <% end %>
    ');
  $('.paginator').replaceWith('<%= escape_javascript(render("shared/house_paginator").to_s) %>');
}

I don't think this is the correct way to accomplish this but not sure what the correct way is?

Anyone have any ideas?

Was it helpful?

Solution

<% if @houses.count > 0 %>
  <% markup = render(@houses) %>
<% else %>
  <% markup = '<div class="message-box"><p>No houses matching those parameters</p></div>'.html_safe %>
<% end %>

function updateContent() {
  $('.organiser__holder').html('<%= escape_javascript render("filter_links") %>');

  $('.houses').html('<%= escape_javascript(markup) %>');
  $('.paginator').replaceWith('<%= escape_javascript(render("shared/house_paginator").to_s) %>');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top