Interpolation of a Ruby string as an ID

I am trying to grasp interpolation and am trying to use a dynamic string ( i say dynamic as it can change all the time, I am pulling dates via a screen scrape for movie releases) and as an ID so that i can use bootstrap scrollspy in my project

I created a js fiddle using conventional ID's and anchor tags that demonstrates what I am trying to achieve.

As you can see, I would like to show the release date of a film if I am within that section as I am scrolling.

My code looks like this:

<div class="container">
 <div class="row">
  <div class="span8 offset2">
   <div id="dateNav">
    <ul class="dateNav">
      <li><a href="##{date}"></a><% @response.each_pair do |date, movie| %><%= link_to date_format(date) %><% end %></li>
   </ul>
   </div>
  </div>
 </div>
</div>
<div class="span9">
 <% @response.each_pair do |date, movie| %>
  <h3 class="resultTitle fontSize13" id="<%= date %>">Available on&nbsp;<%= date_format(date) %></h3>
</div>
<!-- More movie information here
<% end %>

I have my body set like so

<body data-spy="scroll" data-target="#dateNav">

and am calling scrollspy like so

$('.dateNav').scrollspy()

When you are in the relevant section the date is supposed to appear

CSS

ul.dateNav ul li a {
 display:none;
}

ul.dateNav ul > li.active > a{
display:block
color: red;
text-decoration: underline;
}

however when scrolling down the page the dates do not appear.

Any help appreciated, would really like to clarify some understanding here.

Thanks

EDIT

ok so have changed as Joe Pym suggested

<li><% @response.each_pair do |date, movie| %><%= link_to date_format(date), "##{date}" %><% end %></li>

Each date has its own id now which wasnt happening before but still no appearance of the relevant date

HTML now generated

<ul class="dateNav">
 <li>
 <a href="#2013-01-09">9th Jan 2013</a>
 <a href="#2013-01-11">11th Jan 2013</a>
 <a href="#2013-01-18">18th Jan 2013</a>
 <a href="#2013-01-23">23rd Jan 2013</a>
 <a href="#2013-01-25">25th Jan 2013</a>
 <a href="#2013-01-30">30th Jan 2013</a>
 </li>
</ul>

EDIT for Sara

calling scrollspy

$('#spyOnThis').scrollspy();

Nav for dates

<div class="container">
 <div class="row">
  <div class="span12">
   <div id="dateNav">
    <ul class="nav dateNav">
      <li><% @response.each_pair do |date, movie| %><%= link_to date_format(date), "#d_#{date}" %><% end %></li>
    </ul>
   </div>
  </div>
</div>

List of Movies

<div id="spyOnThis">
  <% @response.each_pair do |date, movie| %>
    <h3 class="resultTitle fontSize13" id="d_<%= date %>">Available on&nbsp;<%= date_format(date) %></h3>
    <% movie.each do |m| %>
      <div class="thumbnail clearfix">
        <img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
        <div class="caption pull-right">
          <%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
          <p class="bio"><%= m.bio %></p>
          <p class="resultTitle">Cast</p>
          <p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
          <%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
        </div>
      </div>

    <% end %>
  <% end %>
  </div>

CSS

#spyOnThis {
 height:auto;
 overflow:auto;
}

I set height as auto because the number of results can change every time

ul.dateNav > li.active > a {
display:block;
color: red;
text-decoration: underline;
}

Answers


IDs that start with a number are invalid. Source

Try adding a string constant to the beginning of each ID.

<a href="#d_#{date}"></a>

and

<h3 class="resultTitle fontSize13" id="d_<%= date %>">

Disclaimer: I don't know Ruby.


EDIT

Okay, so I stripped this down and got it working. jsFiddle

I changed some of your classes and IDs for fiddle purposes (also there seemed to be some confusion about .dateNav and #dateNav), but it should look something like this:

<ul class="nav dateNav">
  <li>
    <a href="#d_#{date}"><% @response.each_pair do |date, movie| %><%= link_to date_format(date) %><% end %></a>
  </li>
</ul>

and

<div class="span9">
  <% @response.each_pair do |date, movie| %>
  <h3 class="resultTitle fontSize13" id="d_<%= date %>">Available on&nbsp;<%= date_format(date) %></h3>
</div>

You can put the IDs on div (like in the fiddle) or h3, it doesn't matter. The important part is the CSS and how you initiate ScrollSpy.

If you're calling it with JavaScript ($('#spyOnThis').scrollspy()), you need to attach it to the element that is going to trigger scroll events - not the menu that keeps track. Source Furthermore, you should remove data-spy and data-target from the body element.

Last but not least, the element you're calling .scrollspy() on needs to have a height and overflow: auto; set for the plugin to keep track of the scroll position.


In your ERB, where you have

<li><a href="##{date}"></a>

You aren't interpolating it. Change it to:

<li><a href="#<%= date%>"></a>

EDIT.

Scrap that...just looks like that a tag should be inside the loop?


Need Your Help

Google API Oauth php permanent access

php google-api oauth-2.0 access-token

I am using the google Calendar API. This is what I want, once you give the app the permission, I can always use the app, without the need of giving access everyday. I keep hearing that I need to sa...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.