add <div> to "each block" each 2 results with rails 3 - ruby-on-rails

I have a each block something like:
<% for f in #following %>
<div class="span6">
<%= f.name %>
</div>
<% end %>
html result:
<div class="span6"> John</div>
<div class="span6"> kevin</div>
<div class="span6"> Peter</div>
<div class="span6"> Andrew</div>
.
.
.
I want add a <div class="row"></div> each 2 results something like:
<div class="row">
<div class="span6"> John</div>
<div class="span6"> kevin</div>
</div>
<div class="row">
<div class="span6"> Peter</div>
<div class="span6"> Andrew</div>
</div>
I want show 2 results per/row. How can I do it?

Checkout each_slice
<% #following.each_slice(2) do |followers| %>
<div class="row">
<% followers.each do |f| %>
<div class="span6">
<%= f.name %>
</div>
<% end %>
</div>
<% end %>

Related

Rails: Will paginate with current_user

I'm currently trying to implement will_paginate into my application, the problem that I'm currently facing is that I want to display it to different users, as users can have many wines and orders.
I'm looking for something to combine the current_user.wines with Wine.paginate..
I currently don't have any idea how to implement this
My wine controller:
def index
#wines = current_user.wines
#wines = Wine.paginate(page: params[:page])
end
As you can see I'm currently overriding #wines...
My wine model:
class Wine < ApplicationRecord
enum instant: {Reservieren: 0, Sofort: 1}
belongs_to :user
self.per_page = 1
end
My index view:
<div class="container-small">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Deine Weine
</div>
<div class="panel-body">
<% if !current_user.admin? %>
<% current_user.favorites.each do |favorite| %>
<div class="row">
<div class="col-md-2">
<%= image_tag favorite.wine.cover_photo(:thumb) %>
</div>
<div class="col-md-7">
<h4><%= favorite.wine.wine_name %></h4>
</div>
<div class="col-md-3 right">
<%= link_to "Bestellen", wine_path(#wines), class: "btn btn-form" %>
</div>
</div>
<hr/>
<% end %>
<% else %>
<% #wines.each do |wine| %>
<div class="row">
<div class="col-md-2">
<%= image_tag wine.cover_photo(:thumb) %>
</div>
<div class="col-md-7">
<h4><%= wine.wine_name %></h4>
</div>
<div class="col-md-3 right">
<%= link_to "Bearbeiten", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div>
<hr/>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<%= will_paginate(#wines) %>
UPDATE
Now it's working in the wine controller, but when I want to implement it into the reservations controller, into the your_orders and your_reservations methods, it's not showing up in the view. I'm not getting an error, when I call <%= will_paginate(#wines) %>
Reservations view:
<div class="row" id="orders">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Alle Bestellungen
</div>
<div class="panel-body">
<% #wines.each do |wine| %>
<% wine.reservations.each do |reservation| %>
<div class="row">
<% if reservation.Bearbeitung? %>
<div class="col-md-3">
Reserviert am<br/>
<%= reservation.start_date.strftime('%v') %>
</div>
<% else %>
<div class="col-md-3">
Erwartet am<br/>
<%= reservation.start_date.strftime('%v') %>
</div>
<% end %>
<div class="col-md-2">
<p><%= reservation.status %></p>
<div class="form-inline">
<% if reservation.Bearbeitung? %>
<%= link_to approve_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-up fa-lg"></i> <% end %> |
<%= link_to decline_reservation_path(reservation), method: :post do %> <i class="fa fa-thumbs-down fa-lg"></i> <% end %>
<% end %>
</div>
</div>
<div class="col-md-4">
<%= reservation.bottle %>x
<%= link_to reservation.wine.wine_name, wine_path(reservation.wine) %><br/>
Gesamt: <%= reservation.total %>€
</div>
<div class="col-md-3 pull-right">
Lieferung an<br/><br/>
<span>
<%= link_to user_path(reservation.user) do %>
<%= reservation.user.fullname %><br/>
<% end %>
<%= reservation.user.location %>
</span>
</div>
</div><!-- row -->
<hr/>
<% end %>
<% end %>
</div><!-- panel body -->
</div><!-- panel default -->
</div><!-- col md 12 -->
</div><!-- row -->
</div><!-- row -->
</div><!-- container -->
<%= will_paginate(#wines) %>
Reservations Controller:
def your_orders
#orders = current_user.reservations.order(start_date: :asc)
end
def your_reservations
#wines = current_user.wines.paginate(page: params[:page])
redirect_to root_path unless current_user.admin == true
#count = Reservation.count(:all)
#total = Reservation.sum(:total)
end

Attempting to iterate with ruby thru 4 twitter bootstrap columns

I am attempting to iterate over a post object, and displaying its body attribute in 4 columns, as to behave like so:
<div class='container-fluid'>
<div class=’row’>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
</div>
<div class=’row’>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
<div class='col-lg-3'><%= *post.body* %></div>
</div>
</div>
I have tried the following:
<div class='container-fluid'>
<% #posts.each_slice(4) do |four_posts| %>
<div class="row">
<% four_posts.each do |post| %>
<div class="col-lg-3">
<%= post.body %>
</div>
<% end %>
</div>
<% end %>
</div>
The issue I'm having is that the 'body' content is running in to the adjoining column. Does anyone have any recommendations?

Rails loop, link_to model_view

Rails: Need help looping through model array to link to the show page. I want to show the name, but link to the path. Seems like it should be simple but I have been coding all night and my brain is fried! please help.
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bars_path %>
<% end %>
</div>
</div>
</div>
</div>
This should work:
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar %>
</div>
</div>
<% end %>
</div>
</div>
You could also do <%= link_to bar.name, bars_path(bar) %>, but is prettier to just give the object. Rails will know which Url helper to use given a specific object.
Take a look at the UrlHelper documentation
Try this
<div class="container">
<div class="row">
<% #bars.each do |bar| %>
<div class="col-xs-6 something">
<div class="firstBar">
<%= link_to bar.name, bar_path(bar) %>
</div>
</div>
<% end %>
</div>
</div>

Rails partial view repeat display

Here is part of my index.html
<div class="col-md-8">
<% #books.each do |book| %>
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
&nbsp
<% end %>
</div>
</div>
<% end %>
</div>
Basically, it will display 3 books, and it works fine.
Then, I move that code into _book.html.erb and edit above code into
<%= render #books %>
However, it repeat 3 times, that is, it display 9 books. And the sequence is [1st,2nd,3rd] [1st,2nd,3rd] [1st,2nd,3rd] like this picture.
Update
update index.html.erb
<div class="clearfix">
<div class="col-md-12">
<%= render #books %>
<div class="col-md-4">
<h3>Genre (Click to filter books)</h3>
<br>
<li>
<span class = 'label label-danger'>
<%= link_to "No filter" ,books_path, style: 'color:#FFFFFD' %>
</span>
<br>
<br>
</li>
<% #genres.each do |genres| %>
<li>
<span class = 'label label-primary' style="color:#FFFFFD">
<%= link_to genres.name ,books_path(filter: genres.name),style: 'color:#FFFFFD' %>
</span>
</li>
<br>
<% end %>
</div>
</div>
<!-- clearfix -->
</div>
Try the following:
<div class="col-md-8">
<%= render #books %>
</div>
And make sure <% #books.each do |book| %> and its <% end %> tag aren't in the partial.
Rendering Collections Docs
Edit
#index.html.erb
<div class="col-md-8">
<%= render #books %>
</div>
# _book.html.erb
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
&nbsp
<% end %>
</div>
</div>

Conditional Rails Views

The code below is fast becoming a common theme in my rails app. I have a bunch of conditions in my view for handling empty data, as well as, managing the push and pull of my grid. This will only grow as I begin to add the 3 other status's. My questions is this. What is the best way of managing my grid elegantly either in the view or controller so my views don't become increasingly bloated with conditions?
<% if #jobs.where(status: 'published').size == 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% end %>
<% if #jobs.where(status: 'published').size == 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% elsif #jobs.where(status: 'published').size == 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>
I think you should use switch case statement for managing multiple conditions.
#status = #jobs.where(status: 'published').size
<% case #status %>
<% when 0 %>
<div class="row">
<div class="large-12 columns">
</div>
</div>
<% when 1 %>
<div class="row">
<div class="large-4 push-8 columns">
</div>
</div>
<% when 2 %>
<div class="row">
<div class="large-4 push-4 columns">
</div>
</div>
<% else %>
<% #published_jobs.in_groups_of(3, false) do |row| %>
<div class="row">
<% for job in row %>
<div class="large-4 medium-4 columns">
</div>
<% end %>
</div>
<% end %>
<% end %>

Resources