I am iterating through a database and displaying the results of each in two columns on the same row. Currently, the image is always displayed in the first column of every row. I would like to alternate this so on every other the image is on the right and the description on the left.
<% #guide.in_groups_of(2, false).each do |guide_row| %>
<% for guide in guide_row %>
<div class='row guide-row'>
<div class="col-md-6">
<%= link_to image_tag(guide.image(:large)), guide.image(:xlarge), :class => 'guide-image' %>
</div>
<div class="col-md-6 guide-list ">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href=" #info_<%= guide.id %>">
<h2 class='guide'><%= guide.title %></h2>
</a>
<h4 class='guide'>From <%= guide.date_starting.strftime("%a %b #{guide.date_starting.day.ordinalize}") %> to <%= guide.date_ending.strftime("%a %b #{guide.date_starting.day.ordinalize}") %></h4>
<div class='col-md-12'>
</div>
<div class="accordion" id="categories">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle btn glyphicon" data-toggle="collapse" data-parent="#accordion" href=" #info_<%= guide.id %>">
<i class='glyphicon glyphicon-info-sign info-icon'></i>
</a>
<% unless guide.extra_info.nil? %>
<%= link_to ' ', guide.extra_info, :class => 'glyphicon glyphicon-globe info-icon' %>
<% end %>
<div id="info_<%= guide.id %>" class="accordion-body collapse">
<div class="acccordion-inner">
<% if guide.description.empty? %>
<h4>No description available</h4>
<% else %>
<p class='guide-description'><%= guide.description %></p>
<% end %>
</div>
</div>
</div>
</div>
</div>
</div>
Thanks in advance.
I am not sure how you are iterating through your rows, it's not really clear from the above.
But I would suggest you create 2 partials (small erb files), one for the even row and one for the odd row. Then based on your iterator, you would render either the even partial or the odd partial, each partial have the image and text swapped from each other.
Basically something like this: (not tested, but should give you the idea.
<%= #even_row? ? render 'shared/even_row' : render 'shared/odd_row' %>
to figure out which pair is even vs odd you can do something like this:
<% #guide.in_groups_of(2, false).each_with_index do |guide_row, index| %>
<%= index.even? ? render 'shared/even_row' : render 'shared/odd_row' %>
then you can use index.even?to determine which partial to render.
Hope that helps
Related
I have created an app in ruby on rails and am using bootstrap ,i am using a 2 column layout ,the problem is the post sizes are unequal and an awkward gap ensues which i want to get rid of.
What my index page currently looks like:
What i want my index page to look like:
my index.html.erb
<div class="container">
<% #posts.each_slice(2) do |posts| %>
<div class="posts-wrapper row">
<% posts.each do |post| %>
<div class="col-lg-4 col-md-offset-1">
<%= post.body %>
</div>
<% end %>
</div>
<% end %>
</div>
Move .row out of the loop and it will be fixed
or try the following code this should also work
<div class="container">
<div class="posts-wrapper row">
<% #posts.each do |post| %>
<div class="col-lg-6">
<%= post.body %>
</div>
<% end %>
</div>
</div>
I would have used JQuery Masonry Library
Right now I have the following callout row:
<div class="row">
<div class="large-9 large-offset-2 columns">
<% flash.each do |type, message| %>
<div data-alert class="callout <%= type %> text-center radius" data-closable>
<%= message %>
<button class="close-button" aria-label="Dismiss alert" type="button" data-close>
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
</div>
</div>
When a callout is present, it pushes the content on the page down. I want it to simply overlap, so the callout is on top of the content. Is there a way to do this?
You could position the callout absolutely so that it overlaps the rest of the content in its parent.
I'm trying to make product images appear as a carousel in a bootstrap modal window.
As the code is now I'm only able to see the first image. At the moment there are three images for this product, uploaded with active admin.
I have tried else elsif and index ==1for the else. I have also tried other modification of in the code below.
I'm kind of stuck, can anyone take a look at this and advise me?
** UPDATE see context in comments from #Lucas Costa**
the problem I'm experiencing seems to be because of the CSS, most probably the .carousel-inner
Have anyone ever had similar problem?
<div class="container text-center">
<h1> Click Me </h1>
<!-- Large modal -->
<button class="btn btn-default" data-toggle="modal" data-target=".bs-example-modal-lg">Large modal</button>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<% #product.photos.each_with_index do |photo, index| %>
<div class="carousel-inner">
<% if index == 0 %>
<div class="item active">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% elsif index == 1 %>
<div class="item">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% else index == 2%>
<div class="item">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% end %>
</div>
<% end %>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
</div>
</div>
</div>
The answer depends what you want.
option 1: you just want to handle the first case (note that here I'm using else)
<% #product.photos.each_with_index do |photo, index| %>
<div class="carousel-inner">
<% if index == 0 %>
<div class="item active">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% else %>
<div class="item">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% end %>
</div>
<% end %>
option 2: you want to handle the first and the second case (note that here I'm using elsif and else)
<% #product.photos.each_with_index do |photo, index| %>
<div class="carousel-inner">
<% if index == 0 %>
<div class="item active">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% elsif index == 1 %>
<div class="item">
<%= image_tag(photo.image.url(:large), class: 'img-responsive') %>
</div>
<% else %>
<%# Your other code here %>
<% end %>
</div>
<% end %>
Ok, by looking at your code the problem seems to be in the first lines of the <!-- Wrapper for slides -->
you are writing it like
<% #product.photos.each_with_index do |photo, index| %>
<div class="carousel-inner">
but it should be
<div class="carousel-inner">
<% #product.photos.each_with_index do |photo, index| %>
Otherwise the loop is inserting .carousel-inner to every image and that breaks the Carousel.
Just switch those two lines and it should work. And remember to also switch the </div> and the <% end %> at the bottom.
I'm trying to create a grid with text, not images, similar to the 'content section' in the Zurb template: http://foundation.zurb.com/templates/orbit.html
I would like to create three blocks of text on each row.
I am able to create this, but the problem that arises is that sometimes in my data there is only one or two blocks of text for a row.
For example, if a product is missing it's location, is there a way to move ingredients to take the place of location, or if category and location are missing to move ingredients to the place of category?
I'm using Ruby on Rails and Foundation 5.
Thanks!
<div class="row">
<div class="large-4 columns">
<% unless #product.category.nil? %>
<div class="product-heading">Product Category</div>
<div class="product-value">
<%= #product.category %>
</div>
<% end %>
</div>
<div class="large-4 columns">
<% unless #product.location.nil? %>
<div class="product-heading">Product Location</div>
<div class="product-value">
<%= #product.location.sort.join(", ") %>
</div>
<% end %>
</div>
<div class="large-4 columns">
<% unless #product.ingredients.nil? %>
<div class="product-heading">Product Ingredients</div>
<div class="product-value">
<%= #product.ingredients.sort.join(", ") %>
</div>
<% end %>
</div>
</div>
HTML generated from Max Williams' suggestion.
<div class="row">
<div class="large-4 columns">
<div class="product-heading">Product Category</div>
<div class="product-value">Food </div>
</div>
<div class="large-4 columns">
<div class="product-heading">Product Ingredients</div>
<div class="product-value">Flour, Sugar </div>
</div>
</div>
Javascript to try and remove empty li tag
<script>$('li, p')
.filter(function() {
return $.trim($(this).text()) === '' && $(this).children().length == 0
})
.remove();
</script>
The key is to look for repeated elements, and abstract them out, for example by using a loop. eg
<% product_cells = [["Category", #product.category], ["Location", #product.location_string], ["Ingredients", #product.ingredients_string]] %>
<div class="row">
<% product_cells.each do |title, data| %>
<% unless data.blank? %>
<div class="large-4 columns">
<div class="product-heading">Product <%= title %></div>
<div class="product-value"><%= data %> </div>
</div>
<% end %>
<% end %>
</div>
Note that i've put the test in a different place, so the whole large-4 div is skipped if the data is blank.
This sort of thing #product.location.sort.join(", ") should usually be replaced with a method in the Product model, eg
#in Product
def location_string
self.location.reject(&:blank?).sort.join(", ")
end
def ingredients_string
self.ingredients.reject(&:blank?).sort.join(", ")
end
then you could change the above code to
<% product_cells = [["Category", #product.category], ["Location", #product.location_string], ["Ingredients", #product.ingredients_string]] %>
EDIT: changed string methods to cope with nils
EDIT2: changed my original answer to use the model methods i added.
I am having trouble aligning my caption to the right of my image within a bootstrap thumbnail
My HTML
<div class="container">
<div class="row">
<div class="span8 offset2">
<% #response.each do |f| %>
<% f.each_pair do |k, v| %>
<h3 class="smallTitle">Available on <%= k %></h2>
<% v.each do |film| %>
<div class="thumbnail">
<img src="http://placehold.it/180x200">
<div class="caption">
<%= link_to film[1], film[0] %> : <%= link_to "remind me", "#", :class => 'remind' %>
</div>
</div>
<% end %>
<% end %>
<% end %>
</div>
</div><!--Row-->
</div><!--/container-->
So at the moment my image is to the left and the caption is below the image.. Is there a simple way with css to get the caption to div to sit to the right of the image
You could just float the image and caption within the thumbnail div
<div class="thumbnail clearfix">
<img src="http://placehold.it/180x200" class="pull-left">
<div class="caption" class="pull-right">
<%= link_to film[1], film[0] %> : <%= link_to "remind me", "#", :class => 'remind' %>
</div>
</div>