Simba_loo
Simba_loo

Reputation: 33

Rendering html partial inside js partial leads to crash

I've encountered a rather strange error when trying to render html partial inside a js partial

In my index page I have:

<% content_for :head do %>
  <script type="text/javascript">
    <%= render :partial => "index", :formats => [:js] %>
  </script>
<% end %>

And here is the _index.js.erb file

$(function(){
<% if @badge %>
$('#badge').html("<%= j render :partial => "badge1", :badge=> @badge, :user =>     current_user %>");
$('#badge').show().delay(3000).fadeOut(2000);
<% end %>
$('#Container_2').mixItUp();
$('#Container_2').mixItUp('sort', 'myorder:desc');
});

When I go to the index page, it gives me the error "Missing partial index". The log says that index.js.erb has been rendered. However, if I delete line 3 in the _index.js.erb file, it works fine.

So, even if I somehow messed up with the badge partial (which I doubt I did), why does it say it cannot find the whole partial when this line is present?

Upvotes: 0

Views: 127

Answers (1)

sunnyto
sunnyto

Reputation: 294

Your issue lies with being unable to properly render the badge1 partial. Try the following:

Make sure there are values for @badge and current_user.

'badge1', locals: {badge: @badge, user: current_user}) %>

Upvotes: 1

Related Questions