When looping over Dataobject, how to display 2 records per column? - twitter-bootstrap-3

When looping over a DataObject how do you only display 2x items per column e.g
I need to display the locations as per this design which has 4 columns. I am using boostrap for the columns.
I have a Location.php Dataobject
class Location extends DataObject {
public static $default_sort = 'Sort';
private static $db = array(
'Sort' => 'Int',
'City' => 'Varchar(255)',
'Address' => 'Text',
'PhoneNumber' => 'Varchar(255)'
);
private static $has_one = array(
'SiteConfigExtension' => 'SiteConfig',
);
private static $summary_fields = array(
'City',
'Address',
'PhoneNumber',
);
}
In my template .ss I have this
<% with $SiteConfig %>
<% loop Locations %>
<div class="col-md-3">
<div class="location-$Pos">
<h6>$City</h6>
<div>$Address</div>
<div>$PhoneNumber</div>
</div>
</div>
<% end_loop %>
<% end_with %>
Obviously atm this creates a new column for each record in the Dataobject.

You can use $First $Last and $MultipleOf(x). The below will display 2 columns per row. Change MultipleOf(2) to another number to display a different number of columns per row. MultipleOf(3) = 3 columns per row, MultipleOf(4) = 4 columns per row, etc. You will need to update the css class names.
<% with $SiteConfig %>
<% loop Locations %>
<% if First %>
<div class="row">
<% end_if %>
<div class="col-md-6">
<div class="location-$Pos">
<h6>$City</h6>
<div>$Address</div>
<div>$PhoneNumber</div>
</div>
</div>
<% if MultipleOf(2) %>
</div><% if not Last %><div class="row"><% end_if %>
<% else %>
<% if Last %></div><% end_if %>
<% end_if %>
<% end_loop %>
<% end_with %>

Related

css: how to display two products in one row using each loop (rails)

Need some help in css.
i have an array of products and m displaying it using each loop. I wanna show two products in each row. do you know how to do it? currently i am using 50% width of div so two products will come in div.
Is there any other way?
Here is code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<% #products.each do |p| %>
<div class="products">
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
</div>
<% end %>
<% end %>
</div>
div product_list is main div
div products is 50%.
So it displays 2 div under main.
But this behaves weird when filters based on category etc. Is there any good way for this?
You can use
I many times handle such situation like this, try if it works for you.
pseudo code:
<div id="product_list" >
<% if #products.size <= 0 %>
<h1/>No products found</h1>
<% else %>
<ul><li class="products">
<% #products.each do |p| %>
<%= (count%2==0):'</li><li>':''; %>
<div><%= image_tag p.photos.first.avatar.url(:big) if p.photos.size > 0 %>
</div>
<% end %>
</li></ul>
<% end %>
</div>

link_to current page rails

I was wondering what the syntax is to link to an attribute on the same page
I have a list of dates
<ul>
<% #response.each_pair do |date, movie| %>
<li><%= link_to date_format(date), date, :class => 'scroll_to' %></li>
<% end %>
</ul>
These then have movies underneath each date like so
<% #response.each_pair do |date, movie| %>
<h3 class="resultTitle fontSize13">Available on <%= 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 %>
What i would like to achieve is that when a user clicks on a date in the list then it will take them to that date with movies on the same page
My attribute for each date is
"release_date"
Do i need to link to that, maybe a piece of Jquery aswell to scroll down to that date? or would it jump to the date in one step?
Any advice appreciated, I have linked to other pages before but not the same page like this
EDIT
I have tried this but the page just re renders
<li><%= link_to date_format(date), params.merge(:release_date => 'release_date'), :class => 'scroll_to' %></li>
Am i on the right track?
Thanks
All you need is to give your date a unique identifier and link to that in you list. For instance below we give the h3 for each date an id relative to the date object. The browser knows how to handle internal links and will simply jump to the corresponding identifier. Notice how the id of the field you're linking to gets appended to the end of the url when you click on the link.
<ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
<% end %>
</ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<h3 class="resultTitle fontSize13" id="<%= index %>">Available on <%= 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 %>
For the added JQuery transition you can replace
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
with
<li><%= link_to_function date_format(date), "$('html, body').animate({scrollTop:$('##{index}').offset().top }, 'slow');", :class => 'scroll_to'%></li>
The above isn't exactly a DRY approach, but the important thing to abstract out of that is that you're linking to a unique ID elsewhere on the page and not the code itself.

RoR: why aren't my microposts showing up?

Here is the users show view where they are supposed to show up. ..
edit (I have updated this post slightly you can see it at RoR: How can I get my microposts to show up?)
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<div id="purchases list">
<ol class="microposts">
<%= render #purchases unless #purchases.nil? %>
</ol>
</div>
<div id="sales list">
<ol class="microposts">
<%= render #sales unless #sales.nil? %>
</ol>
</div>
so the forms (partials) are loading fine, but then when I make a post, in either one, neither the purchases list nor the sales list shows up. I checked the database and they are being created along with an entry in the column indicating kind (either sale or purchase)
Here are the forms:
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and
<%= form_for (#micropost) do |f| %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= hidden_field_tag 'micropost[kind]', "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
also, here is the show part of the users_controller.rb
def show
#user = User.find(params[:id])
#micropost=Micropost.new
#microposts = #user.microposts.paginate(page: params[:page])
end
and here is the show part of the microposts_controller.rb
def show
#micropost = Micropost.find(params[:id])
#microposts = Micropost.where(:user_id => #user.id)
#purchases = #microposts.where(:kind => "purchase")
#sales = #microposts.where(:kind => "sale")
end
can anyone help me out? anything else i need? hmmm
First, just to be sure you are getting the results you want, you should try something like this in your view
<%= #sales %>
This should be a hash of the results you want. Then, if that looks good, you want to do something like this
<div id="sales_list">
<ol class="microposts">
<% if #sales.any? %>
<% #sales.each do |sale| %>
<li><%= sale %></li>
<% end %>
<% end %>
</ol>
</div>
And repeat for purchases

RoR: how can I list only microposts that have a certain attribute in one column?

Right now I am listing all of a users microposts using the code below.
<div class="span8">
<% if #user.microposts.any? %>
<h3>Purchases I am interested in (<%= #user.microposts.count %>)</h3>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
<% end %>
</div>
and the view that renders for _micropost.html.erb is as follows
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
so this works fine, however I am changing things up. Every micropost has a hidden_tag_field which is a string (and a column in the database) that is called kind. It can be either "purchase" or "sale". I want to list all of the purchase microposts in one place and all the sale microposts in another. How can I change the micropost view to do this?
Controller:
#purchases = #microposts.where(:kind => 'purchase')
#sales = #microposts.where(:kind => 'sale')
View:
<h2>Purchases</h2>
<%= render #purchases %>
<h2>Sales</h2>
<%= render #sales %>

Rails 3 Assign A Container Div Within Loop

say I'm looping through an array of products, how would I go about assigning a container div within that loop. For instance I want output like so:
<div class="page">
<p>product1</p>
<p>product2</p>
<p>product3</p>
<p>product4</p>
</div>
<div class="page">
<p>product5</p>
<p>product6</p>
<p>product7</p>
<p>product8</p>
</div>
I tried something like this:
<% #products.each_with_index do |product, index| %>
<% if index%4 == 0 %>
<div class="page">
<%end%>
<p><%= product.data %>
<% if index%4 == 0 %>
</div>>
<%end%>
<% end %>
But as expected it will only surround every 4th product with the container. I'm thinking that I'm going to need two loops in order for this to work, but how can I do this without duplicating data?
Edit
When using alternatives to each_with_index is there any way to keep a track of the index using this method? There are also some conditional attributes that get set based on the index value.
For example:
style=<%= index == 0 ? "top:5px;" : ""%>
<% #products.each_slice(4) do |slice| %>
<div class="page">
<% slice.each do |product| %>
<p><%= product.data %></p>
<% end %>
</div>
<% end %>
Another approach:
<% while group = #products.take 4 %>
<div class="page">
<% group.each do |product| %>
<p><%= product.data %></p>
<% end %>
</div>
<% end %>
The Rails method to split an array into groups of equal size is ary.in_groups_of(number)
<% #products.in_groups_of(4, false) do |group| %>
<div class="page">
<% group.each do |product| %>
<p><% product.data %></p>
<% end %>
</div>
<% end %>
Much nicer in Haml :)
- #products.in_groups_of(4, false) do |group|
.page
- group.each do |product|
%p= product.data
You can get the absolute product index with #products.index(product) or the group index with
<% #products.in_groups_of(4, false).each_with_index do |group, group_index| %>
<div class="page">
<% #products.each_with_index do |product, index| %>
<p><% product.data %></p>
<% if index % 4 == 3 %>
</div><div class="page">
<% end %>
<% end %>
</div>
(edit: forgot my end tag!)