Bootstrap Scrollspy final element highlighted - ruby-on-rails

Ok so hoping this is final question on Bootstrap Scrollspy, almost there, one more problem to fix i hope. As I have read having a body of 100% seems to disagree with scrollspy ( Im using sticky footer). The final element in my nav is highlighted no matter where i am on the page.
I have tried removing 100% body
I have tried removing the scrollspy js
I have tried setting the body as the target element
I have tried $('body').scrollspy();
None of these work. If i set the height though on the element I am spying on then it does work, though it seems to scroll past the target element quite a bit and then change. I would like to still be able to keep sticky footer.
Here is my code
View
<div class="container">
<div class="row show-grid clear-both">
<div id="left-sidebar" class="span3 sidebar">
<div class="side-nav sidebar-block">
<div id="dateNav">
<h3 class="resultTitle fontSize13">Release Dates</h2>
<ul class="nav date">
<% #response.each_pair do |date, movie| %>
<li><i class="icon-chevron-right"></i><%= link_to date_format(date), "#d_#{date}", :id=> '#d_#{date}' %></li>
<% end %>
</ul>
</div>
</div>
</div>
<div class="span9">
<div id="spyOnThis" data-spy="scroll" data-target="#dateNav">
<% #response.each_pair do |date, movie| %>
<h3 class="resultTitle fontSize13" id="d_<%= date %>">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<!--Image here
<% end %>>
<div class="caption pull-right">
<!--Content Here
</div>
</div>
<% end %>
<% end %>
</div>
</div><!--span9-->
</div><!--Row-->
</div><!--/container-->
JS
$('#dateNav').scrollspy();
CSS
#dateNav{
position: fixed;
}
#spyOnThis {
height:100%;
overflow:auto;
}
.side-nav .active a {
color: #FFBE00;
}
HTML Output (Nav)
<div id="left-sidebar" class="span3 sidebar">
<div class="side-nav sidebar-block">
<div id="dateNav">
<h3 class="resultTitle fontSize13">Release Dates</h3>
<ul class="nav date">
<li><i class="icon-chevron-right"></i>
<a id="#d_#{date}" href="#d_2013-01-09">9th Jan 2013</a>
</li>
<li><i class="icon-chevron-right"></i>
<a id="#d_#{date}" href="#d_2013-01-11">11th Jan 2013</a>
</li>
<li class="active"><i class="icon-chevron-right"></i>
<a id="#d_#{date}" href="#d_2013-01-30">30th Jan 2013</a>
</li>
</ul>
</div>
</div>
</div>
Element being spied on
<div class="span9">
<div id="spyOnThis" data-target="#dateNav" data-spy="scroll">
<h3 id="d_2013-01-09" class="resultTitle fontSize13">Available on 9th Jan 2013</h3>
<div class="thumbnail clearfix">
<!--Image Here -->
<div class="caption pull-right">
<!--Paragraphs in here -->
</div>
</div>
<h3 id="d_2013-01-11" class="resultTitle fontSize13">Available on 11th Jan 2013</h3>
<div class="thumbnail clearfix">
<!--Image Here -->
<div class="caption pull-right">
<!-Paragraphs here
</div>
</div>
<div class="thumbnail clearfix">
<!-- Image Here-->
<div class="caption pull-right">
<!-paragraphs here -->
</div>
</div>
<div class="thumbnail clearfix">
<!-- Image Here-->
<div class="caption pull-right">
<!-paragraphs here -->
</div>
</div>
<h3 id="d_2013-01-09" class="resultTitle fontSize13">Available on next date</h3>
<div class="thumbnail clearfix">
<!--Image Here -->
<div class="caption pull-right">
<!--Paragraphs in here -->
</div>
</div>
</div>
</div>
Apologies for the amount of code but better to have more on there i guess
So does anyone know of a solution for this as scrollspy does seem quite buggy at the moment
Thanks

You simply can't set 100% height on the element you're spying on with ScrollSpy, whether it be body or another div.
However, there is an issue on GitHub that suggests a workaround for this (also discussed here).
In your case, this would be:
$(window).scrollspy({wrap: $('#spyOnThis')[0]});
Here's a jsFiddle of your code that works with that fix. Note that I changed some of your HTML:
I removed the data-target and data-spy attributes again. When initiating ScrollSpy, use either the data attributes or the JavaScript.
I gave your span9 div the #spyOnThis ID, since the extra markup was unnecessary.
Hopefully this will resolve it once and for all.
EDIT
This solution worked; for #Richlewis' specific scenario we needed to add the parameter offset: -250 to ScrollSpy.

I just got scrollspy working on a site that I'm building. I was having the same issue where the last element was always highlighted. My problem was that I was calling .scrollspy() on the nav element when it should be on the body.
Try changing your script call to:
$('body').scrollspy();

In case the other answers don't work for you, my problem ended up being a missing doctype on the top of the page.
Scrollspy calls $(window).height() in the formula to calculate the current scroll position. From this other question, you can see that this call will not work without a correct doctype.
As soon as I added <!DOCTYPE html> to the top of my HTML, Scrollspy started highlighting the correct links.

Related

My images wont stay on the same line, trying to use bootstrap row

My images wont stay on the same row once i put the <p> in, they both sit in there own row, not sure what im doing wrong here
<div class="container">
<div class="row">
<div class="image-fluid text-center">
<img class="bruno" src="resourcesnew/css/Img/bruno.jpg">
<p class="h6">Bruno</p>
<img class="anth" src="resourcesnew/css/Img/anth.jpg">
<p class="h6">Anthony</p>
</div>
</div>
</div>
I think whatever CSS you used in class="row" is not acting the way you expect, because that div has a single child.
I don't know what's in your css file, but the following HTML works as I expect you want:
<div class="container">
<div class="image-fluid text-center" style="display:flex">
<div>
<img class="bruno" src="resourcesnew/css/Img/bruno.jpg">
<p class="h6">Bruno</p>
</div>
<div>
<img class="anth" src="resourcesnew/css/Img/anth.jpg">
<p class="h6">Anthony</p>
</div>
</div>
</div>
Each image/text pair gets its own div, and what used to be your row div now has multiple children.

How to use link_to in rails to put multiple things in a single link?

This is my code:
<div class="column">
<div class="post-module">
<a href="https://www.livescience.com/59802-should-we-fear-
intelligent-robots.html" >
<div class="thumbnail">
<div class="date">
<div class="day">14</div>
<div class="month">Jul</div>
</div>
<img src="#"/>
</div>
<div class="post-content">
<div class="category">Robots</div>
<h1 class="title">Should We Fear The Rise of Intelligent
Robots? </h1>
<h2 class="sub_title"> Robots will unite. </h2>
<p class="description"></p>
</div>
</a>
</div>
</div>
I want to make that code in rails but I am just a beginner in rails and I wasn't able to find something that could help me in this. I only know the basic idea of the link_to in rails.
link_to method can take a block and display it's evaluation as a link content:
<%= link_to "https://www.livescience.com/59802-should-we-fear-intelligent-
robots.html" do %>
<div class="thumbnail">
<div class="date">
<div class="day">14</div>
<div class="month">Jul</div>
</div><img src="#"/>
</div>
<div class="post-content">
<div class="category">Robots</div>
<h1 class="title">Should We Fear The Rise of
Intelligent Robots? </h1>
<h2 class="sub_title"> Robots will unite. </h2>
<p class="description"></p>
</div>
<% end %>

Trying to create a specific data-target for bootstrap modal through ruby iteration

I am using bootstrap modal on my rails project. On this page I have to iterate through my slides and for each slide I want to create a modal. I came up with this solution:
<% #slides.each do |slide| %>
<div class="row slidecard text-center">
<div class="col-xs-12 ">
<h2><%= slide.name.upcase %></h2>
<p><em><%= slide.description %></em></p>
<a href="#" data-target=<%=slide.id%> data-toggle="modal">
<%= image_tag slide.picture.url(:medium) %>
</a>
<div class="modal fade" id= <%=slide.id%>>
<div class="modal-dialog mymodal">
<div class="modal-content text-center">
<div class="modal-body">
<%= image_tag slide.picture.url(:original) %>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div
</div>
I give to the link containing the image the data-target attr and gives it the value that has the slide.id so I can give a specific id and data target that links image and modal. But the problem is that for the modal to work I need to add "#"in front of slide.id to have something that looks like data-target="#39"
I tried a couple of things such has :
data-target= "#"<%=slide.id%>
and other stuff but nothing gives me the result I want and make the modal work !
licence
try to this

Angular js filter with rails incorrectly functioning

Hi I am trying to use angular filtering in my rails app,but I do not know why it's not behaving in the correct way.
So I have a gender dropdown selection on which i filter my listings array.
Here are the codes:
/listing.html.erb/
<div class="ui page grid">
<div class="column">
<form class="ui form">
<div class="six fields">
<div class="field">
<label>Gender</label>
<%=select(:listing,:gender,options_for_select([['Male','Male',{class:'item'}],['Female','Female',{class:'item'}]]),{prompt:'Gender'},{:'ng-model'=>'listing1.gender',class:'ui dropdown gender'})%>
</div>
</div>
</form>
</div>
</div>
<div class="ui divided items" ng-controller="ListingController" ng-init="init(<%=#listings.to_json %> )" >
<div class="item" ng-repeat="listing in listings | filter:{'gender':listing1.gender}">
{{listing1.gender}}
<div class="image">
</div>
<div class="content listing_content">
<i class="right floated large like icon"></i>
<i class="right floated large star icon"></i>
<%=link_to '{{listing.title}}','listings/{{listing.id}}',class:'header'%>
<div class="meta">
<span class="cinema">Posted On
<%= #date = '{{listing.created_at}}' %>
</span>
</div>
<div class="description">
{{listing.love_for_pets}}
</div>
<div class="extra listing_price">
<div class="right floated ui circular facebook icon button">
<i class="facebook icon"></i>
</div>
<div class="right floated ui circular twitter icon button">
<i class="twitter icon"></i>
</div>
<div class="right floated ui circular google plus icon button">
<i class="google plus icon"></i>
</div>
<div class="ui teal tag label"><i class="rupee icon"></i>{{listing.price}}</div>
</div>
</div>
</div>
</div>
It is getting all the data properly,everything is working fine,except for the filter.When i select female ,it gives me filtered output but for male I get all the listings.
Can someone please help in this.
The reason you're getting all results for "Male" is because the filter, as it's being used, returns partially matching results. To get an exact match within your filtering, modify your ng-repeat to this:
ng-repeat="listing in listings | filter:{gender : listing1.gender : true}
This should return strictly "Male" or "Female", and not match male to the partially matching female results.
Check out the docs on filter, specifically the arguments section that covers this option.
https://docs.angularjs.org/api/ng/filter/filter
Hope it helps!

How do I add left and right classes to every other element in a loop in Ruby on Rails?

I am working within a Ruby on Rails app. I have a loop that creates divs like this:
<% #snorks.each do |snork| -%>
<div>
<%= snork %>
</div>
<% end %>
And I need the output to have every other div be floated left or right like this:
<div class="left">
Allstar Seaworthy
</div>
<div class="right">
Casey Kelp
</div>
<div class="left">
Dimmy Finster
</div>
<div class="right">
Daffney Gillfin
</div>
<div class="left">
Tooter Shellby
</div>
<div class="right">
Dr. / Uncle Galeo
</div>
Additionally, I need to add a div with class="clear" every two divs, like this:
<div class="left">
Allstar Seaworthy
</div>
<div class="right">
Casey Kelp
</div>
<div class="clear"></div>
<div class="left">
Dimmy Finster
</div>
<div class="right">
Daffney Gillfin
</div>
<div class="clear"></div>
<div class="left">
Tooter Shellby
</div>
<div class="right">
Dr. / Uncle Galeo
</div>
<div class="clear"></div>
I have researched, and found a few posts saying that the alternate classes can be accomplished easily by using cycle(), and that does work. However, when I use it in two places within the loop it stops working right and just outputs something like this:
<div class="left">
Allstar Seaworthy
</div>
<div class="left">
Casey Kelp
</div>
<div class="left">
Dimmy Finster
</div>
<div class="left">
Daffney Gillfin
</div>
<div class="left">
Tooter Shellby
</div>
<div class="left">
Dr. / Uncle Galeo
</div>
What's the best practices way in Ruby on Rails to alternate classes in a loop, and also add something every other loop?
According to the documentation, if you need nested ones you name them. Otherwise they will both share the name "default" and conflict.
<% #snorks.each do |snork| -%>
<div class="<%= cycle('left', 'right') -%>">
<%= snork %>
</div>
<%= cycle('','<div class="clear"></div>', :name=>"cleardiv") %>
<% end %>
The best thing here seems to be using each with index. That way you could do some simple modulus math to determine if the number is odd or even and output the correct class and add the clears.
#snorks.each_with_index do | snork, index|
If index%2 == 0
class = 'left'
else
class = 'right'
end
Well u get my drift and I'm on my phone.
use cycle helper
http://apidock.com/rails/ActionView/Helpers/TextHelper/cycle
<% #snorks.each do |snork| -%>
<div class="<%= cycle("left", "right") -%>">
<%= snork %>
</div>
<% end %>
Edit: for adding new div; following could help
<% #snorks.each_slice(2) do |snork_batch| -%>
<% snork_batch.each do |snork|%>
<div class="<%= cycle("left", "right",:name=>"className") -%>">
<%= snork %>
</div>
<%end%>
<div class="clear"></div>
<% reset_cycle("className")%>
<% end %>

Resources