4

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;
}
Richlewis
  • 13,978
  • 32
  • 103
  • 238
  • I don't understand - the links are red in your fiddle. What color are you expecting them to be? – Sara Jan 23 '13 at 21:17
  • sorry my mistake, i have updated question with issue, got 2 issues mixed up there – Richlewis Jan 23 '13 at 21:25
  • Per the fiddle, do you want the date headings to change as you scroll to them or the link in the nav? – Dmase05 Jan 23 '13 at 21:49
  • as i get to the date headings i would like the dates in the nav to change to that date, so if i am in the section where films are released on say 30th Jan 2013, the link in the nav should say 30th Jan 2013 – Richlewis Jan 23 '13 at 21:52
  • @Richlewis I've updated my answer with a working fiddle. – Sara Jan 23 '13 at 22:45

2 Answers2

5

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.

Community
  • 1
  • 1
Sara
  • 8,064
  • 1
  • 32
  • 50
  • thank you Sara for your answer, it doesn’t work but I guess your disclaimer isn’t of a sarcastic nature so I should just play around with some variations or look up string constants in ruby – Richlewis Jan 23 '13 at 21:57
  • Not sarcastic at all - does it output the `d_` IDs as expected? If so, I'm afraid my ability to help ends there. – Sara Jan 23 '13 at 21:59
  • Yes it outputs 23rd Jan 2013 and

    – Richlewis Jan 23 '13 at 22:02
  • Thanks so much Sara, that has cleared up a lot of things there for me. Really appreciate you taking the time to solve it – Richlewis Jan 24 '13 at 08:15
  • apologies for this but would you mind checking your fiddle, doesnt seem to be working for me – Richlewis Jan 24 '13 at 18:26
  • It looks fine to me (nav dates turn red as you scroll past their box), what's not working for you? – Sara Jan 24 '13 at 18:32
  • for some reason when i view the fiddle I can only scroll to the 18th but even then a date doesn't change colour? weird – Richlewis Jan 24 '13 at 18:35
  • can i be a pain and post up some more code, i have followed what you have done but still the dates dont change colour?would you mind? – Richlewis Jan 24 '13 at 18:36
  • @Richlewis you can't use `height: auto;` with a scrollspy target. See related questions [here](http://stackoverflow.com/questions/11909474/why-isnt-bootstraps-scrollspy-working) and [here](http://stackoverflow.com/questions/11658755/in-twitter-bootstraps-scrollspy-where-exactly-can-i-put-data-spy-scroll). If you're still having problems, try the fixes suggested [here](http://stackoverflow.com/questions/12095579/twitter-bootstrap-scrollspy-always-selecting-last-element). You may also want to look into a non-Bootstrap scrollspy solution, as theirs seems to be rather finicky. – Sara Jan 24 '13 at 19:02
  • thanks for those links, I think I may know why its not working, in all the examples i have seen the content scrolls under the navbar, but in my app the content is scrolling underneath it and the nav overlaps the content..hope that makes sense – Richlewis Jan 24 '13 at 19:15
  • and also am i actually mis understanding scrollspy (she thinks yes to herself :) ) does the content have to be in its own scroller and the scroller for the main page is irrelevant – Richlewis Jan 24 '13 at 19:22
1

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?

Joe Pym
  • 1,796
  • 12
  • 23