0

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?

1 Answer 1

1
<% 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) %>');
}
Sign up to request clarification or add additional context in comments.

2 Comments

That works nicely and is 100 times more organised than what I was doing. But when houses = 0 I get: <div class="message-box"><p>No coasters matching those parameters</p></div> output to the page as text?
I modified the answer, try it again?

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.