0
votes

I have the following code inside a js.erb file ::

var title = $('#kase_listing_title_show_determinator').attr('data-known-title-display');
$('#kase_list_core').fadeOut('slow');
$('#kase_list_core').html("<%= escape_javascript(render :partial => 'kase_details', :locals => {:kases => @cases, :displayingtitle => "title" }) %> ");
$('#kase_list_core').fadeIn('slow');

What i am trying to do here is that I am using jquery to read a data-* attribute from a div and based on the value of this attribute I have to hide/show something in the partial.

I want to pass this value as a local variable in my rails code. But every time to parital gets loaded I get:

undefined local variable or method `displayingtitle' for #<#Class:0x007fed82319bc0>:0x007fed88f5ec40> error.

I dont know why the second variable is not getting set.

Maybe i am just doing something silly here. Please help.

PARTIAL CODE :

if i delete the one line that has this variable in it - everything works fine.

<% kases.each do |kase| %>

  <!-- This div is used to determine whether to show or hide the known/ unknown title mode -->
  <!-- Do Not Delete this div - it is used for the kases with video's and show diagnosis check boxes -->
  <div id="kase_listing_title_show_determinator" data-known-title-display="false" data-show-kase-with-video-only="false" ></div>

  <div id="kase_listing_display<%= "#{kase.id}" %>" onmouseover="kase_list_display_mouse_over(<%= "#{kase.id}" %>)" onmouseout="kase_list_display_mouse_out(<%= "#{kase.id}" %>)" class="kase_listing_display" >

    <% if displayingtitle == 0 %>NO<% else %>YES<% end %>

    <div id="kase_listing_known_diagnosis<%= "#{kase.id}" %>" class="mediumtitle gray" style="display:none;">
      <%= kase.knowntitle %>
    </div>

    <br>

    <div id="kase_listing_unknown_diagnosis<%= "#{kase.id}" %>" class="mediumtitle gray" >
      <%= kase.unknowntitle %>
    </div>

    <br>

    <div class="titles gray">
      Author: <%= Author.primaryauthor(kase.id).first.displaystring %>
    </div>

    <br>

    <div class="regularfont gray">
      <strong>Presentation:</strong><%= kase.presentation.truncate(100, :separator => ' ', :ommission => "...").html_safe unless kase.presentation.blank? %>&nbsp;<%= link_to "Read More", kase_path(kase.id), :class => "topmenulinks" %>
    </div>

    <br>

    <!-- # <%= image_tag("#{kase.id}/#{kase.kase_images.first.name}", :width => "150") %><br><br> -->
  </div>
<% end %>
1
Should it be :displayingtitle => title? Remove the quotes. Just a thought. - stephenmurdoch
hi stephen - tried that as well. Doesn't work. Same error. It is as if the second variable is not getting passed to the partial at all. Any ideas would be helpful. - deepinder
Can you post the contents of the partial? - Jacob Gillespie
Sure - so far since i was testing it out - there is only one line in the partial that has this param in it. Everything else works if i delete this one line <% if displayingtitle == 0 %>NO<% else %>YES<% end %> This is the only line where the variable is being called. - deepinder
So, does it work if you make the partial say <%= kases %>? - Jacob Gillespie

1 Answers

0
votes

Have you tried this : "+" around the title:

var title = $('#kase_listing_title_show_determinator').attr('data-known-title-display');
$('#kase_list_core').fadeOut('slow');
$('#kase_list_core').html("<%= escape_javascript(render :partial => 'kase_details', :locals => {:kases => @cases, :displayingtitle => "+title+" }) %> ");
$('#kase_list_core').fadeIn('slow');

For retrieving the data you can also do data() instead of attr():

var title = $('#kase_listing_title_show_determinator').data('known-title-display');