Table in partial view not rendering - ruby-on-rails

I am trying to display a table of jobs for a client sectioned by month year. The partial views evaluate and using binding.pry on each I can see that the correct values are there. The _month_section.html.erb date does not show, nor does the table in _jobs.html.erb
clients_controller.rb
def show
if id = params[:id]
if #client = current_user.clients.find_by_id(id.to_i)
if #client.jobs.count > 0
#jobs_by_month = #client.jobs.group_by{|j| j.created_at.strftime('%B %Y')}
end
else
redirect_to dashboard_path
end
end
end
show.html.erb
<% if #client.jobs.count > 0 %>
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="panel panel-white">
<div class="panel-heading">
<h4 class="panel-title">Jobs</h4>
<div class="panel-body">
<%= render 'month_section' %>
</div>
</div>
</div>
</div>
</div>
<% end -%>
_month_section.html.erb
<div class="month_section" >
<% #jobs_by_month.each do |date, jobs| %>
<div>
<h3><%= date %></h3>
<%= render partial: 'jobs', :locals => {:jobs => jobs } %>
</div>
<% end %>
</div>
_jobs.html.erb
<div class="jobs">
<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
</tr>
</thead>
<tbody>
<% jobs.each do |job| %>
<tr>
<td><%= job.date%></td>
<td><%= job.start_time %></td>
<td><%= job.end_time %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>

Turns out issue was due to a rogue non-terminated div tag on show.html.erb outside of the snippet I posted.

Related

Rails 5 yield gives duplicate contents

I am figuring out why it showing duplicate data on the front page:
All I did is to use Rails collection rendering.
Here's my application.html.erb file:
<body>
<%= render 'layouts/navbar' %>
<!-- content -->
<main class="pt-5">
<div class="container">
<div class="row">
<%= render 'layouts/sidebar' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</main>
</body>
</html>
Here's my index.html.erb file:
<div class="col-md-9">
<% if #contacts.blank? %>
<div class="alert alert-warning">
<strong>Record is empty!</strong>
</div>
<% else %>
<div class="card">
<div class="card-header"><strong>All Contacts</strong></div>
<table class="table">
<%= render #contacts %>
</table>
</div>
</div>
<% end %>
and here's my _contact.html.erb file:
<% #contacts.each do |contact| %>
<tr>
<td class="middle">
<div class="media">
<div class="media-left">
<a href="#">
<%= image_tag contact.gravatar, class: "media-object" %>
</a>
</div>
<div class="media-body">
<h4 class="media-heading"><%= contact.name %></h4>
<address>
<strong><%= contact.address %>, <%= contact.city %>, <%= contact.state %>, <%= contact.zip %></strong><br>
<%= contact.email %> | <%= contact.phone %> | <%= contact.mobile %>
</address>
</div>
</div>
</td>
<td width="100" class="middle">
<div>
<a href="#" class="btn btn-outline-secondary btn-circle btn-xs" title="Edit">
<i class="fa fa-edit"></i>
</a>
<a href="#" class="btn btn-outline-danger btn-circle btn-xs" title="Delete">
<i class="fa fa-times"></i>
</a>
</div>
</td>
</tr>
<% end %>
BTW, I am using Kaminari Gem for pagination. It works however the data shows twice.
Any idea?
UPDATES:
Here's my contacts controller:
class ContactsController < ApplicationController
def index
#contacts = Contact.page params[:page]
end
end
Here's the kaminari config file:
# frozen_string_literal: true
Kaminari.configure do |config|
config.default_per_page = 2
# config.max_per_page = 4
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
# config.params_on_first_page = false
end
When you do <%= render #contacts %>, Rails will loop results from #contacts and render each one using _contact.html.erb, while embedding local variable contact in each one. You see contacts twice, because you loop twice: remove <% #contacts.each do |contact| %> from _contact.html.erb and it should work.

how to search the data based on month in rails

I need to search the data based on
By Month
By quater
By Half Year
By year in my revenue report.
Can any one help me how to do? My revenue report displays the booking information
in controller.
def revenue
#bookings = Booking.all
#bookings = Booking.where(currency: params[:currency]) if param[:currency].present?
end
My revenue.html.erb
<div class="col-md-3 col-lg-2 col-xlg-1">
<%= form_tag revenue_path, :method => 'get' do %>
<%= select_tag(:currency, options_for_select(["USD", "EUR"]), onchange: 'this.form.submit();', class: "form-control custom-select" , include_blank: true ) %>
<% end %>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<%= select_month(Date.today)%>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By quater</option>
</select>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By Halfyear</option>
</select>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By Year</option>
</select>
</div>
<div class="table-responsive">
<table id="demo-foo-addrow" class="table m-t-30 table-hover no-wrap contact-list no-paging footable-loaded footable" data-page-size="2">
<thead>
<tr>
<th class="footable-sortable">#<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">
<input type="checkbox" class="check" id="minimal-checkbox-1">
</th>
<th class="footable-sortable">First Name<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Last Name<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Email<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Status<span class="footable-sort-indicator"></span></th>
</tr>
</thead>
<tbody>
<% #bookings.each do |booking| %>
<tr>
<td><%= booking.id %></td>
<td><%= check_box_tag "bookings[booking_ids][]", booking.id %></td>
<td><%= booking.first_name %></td>
<td><%= booking.last_name %></td>
<td><%= booking.email %></td>
<td><%= booking.status %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
Can anyone help me out. Thanks in advance!
Based on debugging together with OP, found that the given method actually does much more than written. In last line of method, #booking = Booking.paginate(:per_page => 100) was being done which dissolved the query set generated earlier.
The final variant looked something like this:
def revenue
#bookings = Booking.all
if params[:currency].present?
#bookings = #bookings.where(currency: params[:currency])
end
if params[:search].present?
#bookings = #bookings.where('first_name like ?', "%#{params[:search]}}")
end
if params['date'] && params['date']['month']
#selected_date = Date.parse('2018-'+ params['date']['month'].to_s + '-01')
#bookings = #bookings.where('extract(month from created_at) = ?', params['date']['month'])
end
if params['date'] && params['date']['year']
#selected_year = params['date'] && params['date']['year'] ? Date.parse( params['date']['year'].to_s + '-01') : Date.today
#bookings = Booking.where('extract(year from created_at) = ?', params['date']['year'])
end
#per_page = params[:per_page] || Booking.per_page || 20
#bookings = #bookings.paginate(:per_page => #per_page, :page => params[:page])
end

Iterate 4 Arrays with Rails

I'm new to Rails and I'm trying to build a nice application and I'm struggling with arrays, I have 4 arrays that I want to iterate and they are not the same size
I want to generate sections in HTML using the first array what I did it
#sections = ['Section One','Section Two','Section Three','Section Four']
#itemsOne = ['item 1','item 2','item 3','item 4','item 5','item 6']
#itemsTwo = ['item 1','item 2','item 3','item 4','item 5','item 6']
I was using
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t1, t2, t3| %>
<%= t1 %>
<table>
<tbody>
<tr>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
<td>
<%= t2 %> | <%= t3 %>
</td>
</tr>
</tbody>
</table>
<% end %>
I have a table that have a Section Title and cells that have two values
but what I get is the value of |t2| in each cell of |t1| section
using #Phil answer down below, but he deleted it.
<%= #sections.zip(#itemsOne, #itemsTwo).each do |t| %>
<%= t[0] %>
<table>
<tbody>
<tr>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
<td>
<%= t[1] %> | <%= t[2] %>
</td>
</tr>
</tbody>
</table>
<% end %>
p.s. the itemsOne and itemsTwo arrays have more than 20 values.
What I created is separated my big arrays into smaller ones and then Iterated thru each this way without a Table because table was making design issues so i went into div's using bootstrap 3 column, there might be a better way but this is what i got as a beginner.
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[0] %></h4>
<!-- This will Display Section 0 in the Array -->
</div>
<div class="row">
<div class="col-md-12">
<% #count = 0 %>
<!-- Counter is Zero -->
<% #ItemsOne.collect do |t1| %>
<!-- This will loop array to increment the #count and repeat the HTML -->
<% #count += 1 %>
<!-- With each loop increment by 1-->
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #ItemsOne[#count - 1] %>
<!-- Counter should start from 0 adding -1 will make it start
from 0 instead of 1 and then will print the value of the Index Number -->
</label>
</div>
</div>
<% end %>
</div>
</div>
</div>
Here is another way to do this
<div class="row">
<% #Sections.each_with_index do |x1, n| %>
<div class="row">
<div class="col-md-12">
<h4><%= #Sections[n] %></h4>
</div>
<div class="row">
<div class="col-md-12">
<% if n == 0 %>
<% #itemsOne.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsOne[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 1 %>
<% #itemsTwo.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsTwo[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 2 %>
<% #itemsThree.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsThree[n] %>
</label>
</div>
</div>
<% end %>
<% elsif n == 3 %>
<% #itemsFour.each_with_index do |t1, n| %>
<div class="col-md-3">
<div class="col-md-12">
<label>
<input type="checkbox" name="optionsCheckboxes">
<%= #itemsFour[n] %>
</label>
</div>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>

advanced Search (checkboxes and selection)

I'm having a problem with my advanced search.
Essentially, its an advanced search for bands (name, style, bandpicture by checkboxes (checked: have a bandpicture))
Here is the code.
result.html.erb
<table >
<colgroup>
<col width="250" />
</colgroup>
<tr>
<td>
<label for="name">Filter</label></br>
<%= form_tag band_path do %>
<label for="Style">Style</label></br>
<%= select_tag :style_id, options_from_collection_for_select(Style.all, :id, :name, params[:style_id]), include_blank: true, class: "form-control" %>
<br>
</br>
<%= check_box_tag 'options[]', "picture" %>
<%= button_to '#', class: 'btn btn-default' do %>
<%= t(:find) %>
<% end %>
<% end %>
</td>
<td>
<div>
<% #bands.each do |band|%>
<div class="top-bar">
<div class="center">
<div class="info">
<div class="name"><%=band.id %></div>
<div> <%= band.zip %>, <%= band.city %> </div>
</div>
<div class="desc">
<table>
<tr>
<td>Styles</td>
<td>
<% band.styles.each do |s| %>
<%= s.style.name %>
<% end %>
</td>
</tr>
</table>
</div>
<% end %>
</td>
</tr>
</table>
models band.rb
def self.search1(style_id)
Profile.joins("...").where("style_id = ?", style_id)
end
controller.
def searchresult
if params[:style_id].present?
#bands = Band.search1 params[:style_id]
elsif params[:options]
Band.where(:options => #filtered_options)
else
#bands = Band.all
end
end
The searchresult don't show the result of the checkboxes and I have this errors:
undefined method `search1' for #<Class:0x00000008eb5548>

Print HTML Page Twitter bootstrap

I have a show page /invoices/show that displays contents of my Invoice
<p id="notice"><%= notice %></p>
<div class="row">
<div class="span7">
<h2 style="text-align:center;"> CONSTRUCTION LTD </h2>
<h3 style="text-align:center;">OHIO</h3>
<address style="text-align:center;"> Plot 10<br/>
</address>
<h3 style="text-decoration:underline; text-align:center;"> UTILITY BILL </h3>
<h4 style="text-decoration:underline; text-align:center;"> TAX INVOICE </h4>
<table>
<td valign="top" align="left">
<div style="float:left; width:450px;">No: <%= #invoice.id %></div>
<div style="float:right"> Date: <%= #invoice.invoice_date %></div>
</td>
</table>
<P></P>
<P></P>
<div>To: <%= #invoice.customer.name%></div>
<p></p>
<table class="table table-bordered table-condensed">
<thead>
<tr class="success">
<th><label class="control-label">Description</label></th>
<th><label class="control-label">Rate</label></th>
<th><label class="control-label">Tax</label></th>
<th><label class="control-label">Amount</label></th>
</tr>
</thead>
<tbody>
<% #invoice.invoice_items.each do | item| %>
<tr class="controls">
<td><%= item.description %></td>
<td><%= item.rate %></td>
<td><%= item.tax_amount %></td>
<td><%= item.amount %></td>
</tr>
<tr>
<td colspan="3" align="left"><strong>TOTAL:</strong></td>
<td colspan="1"><%= item.amount %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="row">
<div class="span3">
<table>
<tbody>
<tr>
<td> <b>For Landlord</b></td></tr>
<tr> <td>Approved by:</td></tr>
<tr> <td>Sign:</td></tr>
</tbody>
</table>
</div>
<div class="span3" style="position: relative; align:left; left:150px;">
<table>
<tbody>
<tr>
<td> <b>For Tenant</b></td></tr>
<tr> <td>Approved by:</td></tr>
<tr> <td>Sign:</td></tr>
</tbody>
</table>
</div>
</div>
<br/>
<br />
<div>
<small><strong>Terms and Conditions</strong></small>
<table>
<tbody>
<tr>
<td><%= Settings.invoice_terms%></td>
</tr>
</tbody>
</table>
</div>
<br />
<div class="form actions">
<p>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_invoice_path, :class => 'btn btn-primary' %>
</p>
</div>
</div>
</div>
In my application.html.erb, I have this for CSS
<%= stylesheet_link_tag "application", :media => "all" %>
More to that, my application file has a nav-bar element.
I am trying to print the Invoices/show.html.erb page by going to the print option in a browser, however, I do not want it to include the nav-bar element in my application.html.erb file and the edit button in invoices/show.html.
I am using Twitter bootstrap, how can i go about this?
Here's one solution I've thought of:
What you can do is, in your show view (or the application layout) add a predicate:
if params[:controller] == "invoice" && params[:action] == "show"
# do or show whatever you want
end
Or you could do add a different layout to your views/layouts folder. Then in your controllers/invoice_controller
def show
# ...
render layout: 'a_different_layout_for_invoices'
end

Resources