Rails 5 - table row while no entry exists - ruby-on-rails

In my rails app, content of documents are being tagged. For documents that do not have any tags, an empty row is shown in the listing (table rows) - always at the bottom of the table. I've tried to solve this for hours, not sure where to research and I'm not getting anywhere. All help welcome!
this is the view (snippet):
<div class="row" id="annotationResults">
<div class="panel panel-default" style="background-color: white; word-wrap: break-word; font-size: 0.9em;">
<table id="tags" class="table table-hover">
<thead>
<tr>
<th>Tagged content</th>
<th>as</th>
<th>in</th>
<th></th>
</tr>
</thead>
<tbody>
<% #annotation.tags.each do |tag| %>
<tr>
<td><%= tag.content %></td>
<td><%= tag.tagtype_id %></td>
<td><%#= tag.tagtype.name %></td>
<td><%= link_to '', [tag.annotation, tag], method: :delete, data: { confirm: 'Please confirm deletion!' }, :class => "glyphicon glyphicon-remove" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>

It's there because you're printing an empty td.
<td><%= tag.content %></td>
Will print the td element even if the tag.content is empty. Check the example:
td{ border: 1px solid black }
<table>
<tr>
<td>foo</td>
<td>bar</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
Use your RoR snippet as you mentioned in the comment:
<% #annotation.tags.each do |tag| %>
<% unless tag.content.blank? %>
<td>
<!-- logic -->
</td>
<% end %>
<% end %>

try this,
<% if tag.content.present? %>
<%= tag.content %>
<% end %>

Related

Missing Partial for render in rails

I create partial which calling _topics_list.form.html.erb and have some code
<table class="list" cellspaceing="0">
<thead class="head_list">
<tr>
<th class="column">Name</th>
<th class="column">Description</th>
</tr>
</thead>
<% #topics.each do |topic| %>
<tr class="row">
<td data-label="Name" class="column first"><%= topic.name %></td>
<td data-label="Description" class="column column_problem">
<%= link_to topic.description, topic_path(topic), class:"link_for_column_problem" %>
</td>
<td data-label="Date" class="column"><%= topic.created_at.strftime("%d %b, %Y") %></td>
</tr>
<% end %>
</table>
Also I connect this partial with my index-file by <%= render 'topics_list'%>
When I try it I have error
Missing partial topics/_topics_list, application/_topics_list with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :arb, :jbuilder]}. Searched in:
* "/home/dse/BlackListV2/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/activeadmin-2.6.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/kaminari-core-1.2.0/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/devise-4.7.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/actiontext-6.0.2.1/app/views"
* "/home/dse/.rvm/gems/ruby-2.7.0/gems/actionmailbox-6.0.2.1/app/views"
UPDATE
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics_list'%>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #topics.each do |topic| %>
<tr>
<td><%= link_to 'Show', topic %></td>
<td><%= link_to 'Edit', edit_topic_path(topic) %></td>
<td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Topic', new_topic_path %>
Code where I render partial
Your partial name is _topics_list.form.html.erb. To render this, you need to call render 'topics_list.form', not render 'topics_list'.
Try changing
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics_list'%>
to
<p id="notice"><%= notice %></p>
<h1 class="request_headline">All Topics</h1>
<%= render 'topics/topics_list' %>
Also, a good idea will be to always pass local variables to partials instead of using global variables, that way you have an absolute control over values in partials.
<%= render 'topics/topics_list', topics: #topics %>
and then in partial use
<table class="list" cellspaceing="0">
<thead class="head_list">
<tr>
<th class="column">Name</th>
<th class="column">Description</th>
</tr>
</thead>
<% topics.each do |topic| %>
<tr class="row">
.....
.....
</tr>
<% end %>
</table>

Layout not rendering properly as expected

In my index action I have
#posts = Post.all
This is my index.html.erb.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<%= #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>
It is rendering fine except that the #posts object is printing in a form like below just above the table tag in my browser. I know it is something silly but I can't figure that out.
[#<Post id: 4, title: "new title", body: "this is new body", created_at: "2015-12-21 07:44:42", updated_at: "2015-12-21 10:31:31">, #<Post id: 7, title: "new title", body: "dfdsfd", created_at: "2015-12-21 09:40:01", updated_at: "2015-12-21 09:40:01">]
Turns out it was something really really silly! The erb template engine requires = with erb tags to display the data while the logic is written without using the =.
It was just a simple change like this:
<% #posts.each do |post| %> # without the = symbol
hai just remove the '=' in the #posts.each line.
<%#posts.each do |post| %>, thats it.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>

Ruby display array values

I am not a ruby developer. This is the first time I am looking into the code. I want to build a dynamic table for which I have managed below code. However, I am not able to display all the contents of the array except for the first and the last values. How do I display all the values?
Thanks for your help!!
<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
<th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
<% tempTickets = #subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
<% cnt = tempTickets.length %>
<% tempTickets.each do |ticket| %>
<div>
<tr>
<td><%= ticket['perfinaccnt-accountname'].first %></td>
<td><%= ticket['perfinaccnt-accountnumber'].first %></td>
<td><%= ticket['perfinaccnt-accountstatus'].first %></td>
<td><%= ticket['perfinaccnt-producttype'].first %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].first %></td>
</tr>
</div>
<div>
<tr>
<td><%= ticket['perfinaccnt-accountname'].last %></td>
<td><%= ticket['perfinaccnt-accountnumber'].last %></td>
<td><%= ticket['perfinaccnt-accountstatus'].last %></td>
<td><%= ticket['perfinaccnt-producttype'].last %></td>
<td><%= ticket['perfinaccnt-accountsrcsystem'].last %></td>
</tr>
</div>
<% end %>
</table>
</div>
You could do it with three nested loops:
<%= tempTickets.each do |ticket|
[
'perfinaccnt-accountname',
'perfinaccnt-accountnumber',
'perfinaccnt-accountstatus',
'perfinaccnt-producttype',
'perfinaccnt-accountsrcsystem'
].each do |f|
ticket[f].each do |tf|
puts "<td>tf</td>";
end if ticket[f]
end
end %>
I'm not sure about the data structure here but you could check it by doing an #inspect to tempTickets and all the subsequent objects. From the looks of it "ticket['perfinaccnt-accountname']" is actually an array
and as such you can just loop through it. Not the fastest way to do it but it should work to just do a nested loop.
<style>table, td, th{border:1px solid white;}td{padding:5px;}th{background-color:#E0E6EB;color:black;}</style>
<div>
<table border=2>
<tr>
<th width="250px"><B><p style="text-align: center">Name</p></B></th>
<th width="120px"><B><p style="text-align: center">Number</p></B></th>
<th width="60px"><B><p style="text-align: center">Status</p></B></th>
<th width="155px"><B><p style="text-align: center">Product Type</p></B></th>
<th width="60px"><B><p style="text-align: center">Source</p></B></th>
</tr>
<% tempTickets = #subject.PersonAccounts.sorted_by(field("title").in_descending_order) %>
<% cnt = tempTickets.length %>
<% tempTickets.each do |ticket| %>
<div><tr>
<% ticket.each do |k,v|
v.each do |col| %>
<td><%= col %></td>
<% end %>
<% end %>
</tr></div>
<% end %>
</table>
</div>

dataTables columnFilter no way to enter data for column filtering

The page shows foot with column names but no places to enter the values for column filters.
I looked to the examples on the data tables web site and others in the stack overflow website and found no clue how to fix it.
Any help or suggestion how to fix this will be much appreciated.
Thank you.
<% provide(:title, 'TBMs Daily Progress') %>
<% if signed_in? %>
<h3>TBMs Max Station by day - Today's date: <%=#current_date%></h3>
<% if #current_date != #tbms_progress_W.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM W is not loaded" %>
<%= " Last TBM W recorded date was: " %>
<%=#tbms_progress_W.daily_date %>
</h6>
<% end %>
<% if #current_date != #tbms_progress_E.daily_date %>
<h6 class="error-message">
<%= "Today's location for TBM E is not loaded" %>
<%= " Last TBM E recorded date was: " %>
<%=#tbms_progress_E.daily_date %>
</h6>
<% end %>
<p>
<% if current_user.admin? %>
<%= link_to "Add new location", new_tbms_progress_path, class: "btn btn-sm btn-danger" %>
<% end %>
<%= link_to "List view", tbms_progresses_path, class: "btn btn-sm btn-primary" %>
</p>
<table id="progresstable" class="display table-bordered table-condensed table-responsive table-hover">
<thead style="background-color: #bbbbbb;">
<tr>
<th>Daily Date</th>
<th>TBM ID</th>
<th>Max Station</th>
<th>TBM Status</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<% #tbms_progresses.each do |tbms_progress| %>
<tr>
<td><%= tbms_progress.daily_date %></td>
<td><%= tbms_progress.tbm_id %></td>
<td><%= number_with_precision tbms_progress.max_station, precision: 2 %>
<td><%= tbms_progress.tbm_status %></td>
<td><%= link_to tbms_progress.id, tbms_progress %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<th>Daily Date</th>
<th>TBM ID</th>
<th>Max Station</th>
<th>TBM Status</th>
<th>ID</th>
</tr>
</tfoot>
</table>
<hr/>
<h6>Legend: W aka TBM1 or 26900, E aka TBM2 or 27000</h6>
<% else %>
<%= render 'instruments/unsigned' %>
<% end %>
<script>
$(document).ready
(function()
{
$('#progresstable').dataTable
(
{
/*sPaginationType: "full_numbers",*/
bJQueryUI: true,
aaSorting: [ [0,"desc"], [1, "desc"] ],
bDeferRender: true,
bStateSave: true
}
).columnFilter
(
{aoColumns:
[
{ type: "text" },
{ type: "text" },
{ type: "text" },
{ type: "text" },
{ type: "text" }
]
}
);
}
);
</script>
Sorry, didn't know a plugin like this existed.
I have checked it out and it works perfectly good.
Look for yourself
Only thing i can imagine ist that you did not include the plugincode from here after jquery and datatables code has loaded, but before you start the initialisation.
<script src="http://code.jquery.com/jquery-2.0.3.min.js" data-semver="2.0.3" data-require="jquery#*"></script>
<link data-require="datatables#*" data-semver="1.9.4" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/css/jquery.dataTables.css" />
<script data-require="datatables#*" data-semver="1.9.4" src="//cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/jquery.dataTables.js"></script>
<script src="colfilter.js"></script>
<script src="script.js"></script>
I solved this by moving to the data tables 1.10.2, not using anymore bootstrap layout for the tables and I running the scripts for data tables from //cdn.datatables.net on each page containing a table.

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