0

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 %>
Jacob Gillespie
  • 3,981
  • 3
  • 23
  • 33
deepinder
  • 131
  • 2
  • 8
  • Should it be `:displayingtitle => title`? Remove the quotes. Just a thought. – stephenmurdoch Aug 16 '12 at 22:01
  • 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 Aug 16 '12 at 22:04
  • Can you post the contents of the partial? – Jacob Gillespie Aug 16 '12 at 22:20
  • 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 Aug 16 '12 at 22:22
  • So, does it work if you make the partial say `<%= kases %>`? – Jacob Gillespie Aug 16 '12 at 22:32
  • Hi Jacob - right - the rest of the code works fine and I am using the :kases variable successfully in the partial as you guessed. I was thinking that maybe i cannot pass a js variable in rails that is why the second local variable is not getting set. But even if i set another @var in the controller and assign that to the second local variable - even that is not getting passed. The problem is that the second local variable is not getting set when i am rendering the partial. – deepinder Aug 16 '12 at 22:42

1 Answers1

0

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');
maiis
  • 735
  • 1
  • 6
  • 13
  • Hey Mais - tried it your way bro but the error is the same. The attr call is working fine. I used alert to see the contents and it is working correctly. The only issue is that somehow the second variable is not getting set. – deepinder Aug 16 '12 at 22:25