Attempting to iterate with ruby thru 4 twitter bootstrap columns - ruby-on-rails

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?

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

Rounded avatar ruby on rails

I would like to have my avatars that my users upload to be round on my rails app project for Udemy. The image still comes out square the way it was uploaded.
below is my show.html.erb file
<div class="row">
<div class="col-md-3 text-center">
<div class="avatar"><%= image_tag #user.profile.avatar.url %></div>
</div>
<div class="col-md-6">
<h1><%= #user.profile.first_name %> <%= #user.profile.last_name %></h1>
<h3><span class="job_title_icon"><%= job_title_icon %></span> <%= #user.profile.job_title %></h3>
<div class="well profile-block profile-description">
<h3>Background</h3>
<%= #user.profile.description %>
</div>
<% if current_user.plan_id == 2 %>
<div class="well profile-block contact-info">
<h3>Contact:</h3>
<%= #user.profile.phone_number %><br/>
<%= #user.profile.contact_email %><br/>
</div>
<% end %>
</div>
Below is my users,css.scss file
<div class="row">
<div class="col-md-3 text-center">
<div class="avatar"><%= image_tag #user.profile.avatar.url %></div>
</div>
<div class="col-md-6">
<h1><%= #user.profile.first_name %> <%= #user.profile.last_name %></h1>
<h3><span class="job_title_icon"><%= job_title_icon %></span> <%= #user.profile.job_title %></h3>
<div class="well profile-block profile-description">
<h3>Background</h3>
<%= #user.profile.description %>
</div>
<% if current_user.plan_id == 2 %>
<div class="well profile-block contact-info">
<h3>Contact:</h3>
<%= #user.profile.phone_number %><br/>
<%= #user.profile.contact_email %><br/>
</div>
<% end %>
</div>
I think setting your avatar class so that the image is round is a better way to approach it.
In your CSS, you define it as such:
.avatar {
border-radius: 50%;
// also might be good to set width and height
}
It looks as if you might be using Twitter Bootstrap. If so, there is a handy CSS class built in.
Just add class: 'img-circle' to your image_tag
...
#change this line as follows
<div class="avatar"><%= image_tag #user.profile.avatar.url, class: 'img-circle' %></div>
...
This page of the Bootstrap docs has lots of useful info and examples.

Missing partial invoices/__invoice_item_fields

i have invoice and invoice_items models in my application. and i have used cocoon gem for nested models.i am using rails 4.2. it is working properly when i am creating new invoice, but when i am clicking on my edit button i am getting "template missing error" though i have _invoice_item_fields.html.erb file in my application.
this is my _form.html.erb file
<div class=" form">
<%= form_for(#invoice,:html=>{:class=>"cmxform form-horizontal tasi-form",:novalidate=>"novalidat"}) do |f| %>
<% if #invoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
<ul>
<% #invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group ">
<%= f.label :billing_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :billing_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :shipping_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :shipping_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :company_id ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :company_id,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :invoice_date ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.date_select :invoice_date,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :status ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :status,:class=>"form-control" %>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
Invoice Items
<span class="pull-right">
<%= link_to_add_association 'Add Item', f, :invoice_items,:class=>"btn btn-default"%>
</span>
</header>
<div class="panel-body">
<div class="adv-table">
<%= f.fields_for :invoice_items do |item| %>
<%= render '_invoice_item_fields', :f => item %>
<% end %>
</div>
</div>
</section>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= f.submit "Save",:class=>"btn btn-danger"%>
</div>
</div>
<% end %>
</div>
this is my _invoice_item_fields.html.erb file
This...
<%= render '_invoice_item_fields', :f => item %>
must be
<%= render 'invoice_item_fields', :f => item %>
You do not use a leading _ in your Rails render call to render a partial. The underscore goes on the filename on disk only.

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 %>

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

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 %>

Resources