Rails 3 View - Rounding Up - ruby-on-rails-3

I have a Rails view to display a grid by PERIOD and GENERAL LEDGER, and I want to display a cumulative total. In the case below, however, my #cumulative_total in the "TOTAL" column is rounding up to the nearest dollar, so no cents are shown, even though they are displayed correctly in the general_ledger columns. Any ideas what I'm doing wrong?
<% #cumulative_total = 0 %>
<div id="gl_crosstab">
<table>
<tr>
<th>Period</th>
<% #general_ledgers.each do |g| %>
<th><%= g.general_ledger_number %></th>
<% end %>
<th>Total</th>
<th>% Expended</th>
</tr>
<% #expected_billings.group_by(&:period_id).each do |eb| %>
<tr>
<td><%= eb[1].first.period.pe_number %></td>
<% eb[1].each do|p| %>
<td><%= number_to_currency(p.expected_amount) %></td>
<% end %>
<td>
<% #cumulative_total = #cumulative_total + eb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i} %>
<%= number_to_currency( #cumulative_total ) %> </td>
<td><%= number_to_currency((#cumulative_total/#sla.project_total)*100) %> % </td>
</tr>
<% end %>
<tr>
<td><b>Total Budget</td>
<% #total_expected_billings.each do |teb| %>
<td><b><%= number_to_currency(teb[1].inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %></td>
<% end %>
<td><b><%= number_to_currency(#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}) %> </td>
<td><b><%= number_to_currency((#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i}/#sla.project_total)*100) %> % </b></td>
</tr>
</table>
</div>

I think that is because you are converting the billing amount to 'integers' while displaying:
#expected_billings.inject(0){|sum,billing| sum+billing.expected_amount.to_i
If you change
billing.expected_amount.to_i
to
billing.expected_amount.to_f
it should work.

Related

Rails - how do I create a hyperlink to a page for a single record?

How do you make <%= post.name %> a link to its own page?
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
</tr>
<% end %>
similar what this code does, but without the string 'Show'
<td><%= link_to 'Show', post %></td>
what i always had in mind is like how php does it, you wrap it in
<a href="<?php phpcode() ?>">
how can i do similar result in ruby?
How about
<%= link_to post.name, post_path(post) %>
or even easier:
<%= link_to post.name, post %>

Rails form associations

I currently have a form in my application that uses select form helpers. It accesses 2 variables:
#user = ['JM', 'JH', 'RY']
#name = ['JASON', 'JOHN', 'RYAN']
My form looks like:
<%= form_for(:event, :url => {:action => :update, :id => #event.id}) do |e| %>
<table>
<tr>
<th>Initails</th>
<td><%= e.select(:initials, (#user)) %></td>
<th>Priroty</th>
<td><%= e.select(:priroty, (1..10)) %></td>
</tr>
<tr>
<th>Name</th>
<td><%= e.select(:name, (#name)) %></td>
<th>Event</th>
<td><%= e.text_field(:event_name) %></td>
</tr>
<tr>
<th>Deadline:</th>
<td><%= e.datetime_select(:deadline, :order => [ :month, :day]) %></td>
<th>Complete ?</th>
<td><%= e.check_box(:complete) %></td>
</tr>
<tr>
<th>Event Description</th>
<td><%= e.text_area(:event_description, :size => '80x10') %></td>
</tr>
<tr>
<th>Comments</th>
<td><%= e.text_area(:comment, :size => '80x10') %></td>
</tr>
</table>
<table>
<div class="actions">
<%= e.submit %>
</div>
</table>
<% end %>
When I select my initials and it shows #user, I am trying to populate the name field that uses #name. Does anybody know how to associate the 2 so when I select JM for instance it pulls up Jason in the name field?
There is no way to do it without javascript.
Take a look at observe_field
And this tutorial

Convert from haml to erb

I try to convert the code from haml to erb but I'm getting stuck and don't know why.
Here is the original code I want to convert: https://github.com/gmarik/simple-backend-example/blob/master/app/views/backend/resource/_index.html.haml
And here is what I have right now. Can someone take a look at it and give me some hints. Thanks.
I doubt this line the most:
%tr[resource]{odd_or_even}
I think that it might be like:
<tr> <% #resource{odd_or_even} %>
RubyMine gave me an error at this line:
<%= paginate collection %>
<% content_for(:header) do %>
<h1><%= resource_class.model_name.human(count: 2) %></h1>
<ul class="tabs">
<li class="active"><%= link_to "Index", "#" %></li>
<li><%= link_to "New", new_resource_path %> </li>
</ul>
<table class='zebra-striped'>
<thead>
<tr>
<% attributes.each do |attr| %>
<th> <%= resource_class.human_attribute_name(attr) %></th>
<th> </th>
</tr>
</thead>
<tbody>
<% collection.each do |resource| %>
<tr> <% #resource{odd_or_even} %>
<% attributes.each do |attr| %>
<td> <%= resource.public_send(attr).to_s.truncate(20) %> </td>
<td class='row-actions'>
<%= link_to 'show', resource_path(resource) %>
|
<%= link_to 'edit', edit_resource_path(resource) %>
|
<%= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?" %>
</td>
<% end %>
</tbody>
</table>
<%= paginate collection %>
Looking at the documentation here : http://haml.info/docs/yardoc/file.REFERENCE.html#object_reference_
I'd say it's :
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= resource.class.name.underscore %>">
This is to translate %tr[resource].
Now, the {odd_or_even} will just convert the result hash of the helper odd_or_even to map them as attributes on the tr.
If we take a look at the definition of the method here : https://github.com/gmarik/simple-backend-example/blob/master/app/helpers/backend/application_helper.rb
We see it's just a call to cycle in order to set an extra class. Therefore we end up with:
<tr id="<%= "#{resource.class.name.underscore}_#{resource.to_key}" %>" class="<%= "#{resource.class.name.underscore} #{cycle("odd", "even", name: "rows")}" %>">
Now, all of this won't fix the problem of paginate. Add the error message if you're still stuck on this.

Loop and generate table cells

I have a table that looks like the following:
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
</tbody>
</table>
Which outputs the following:
I am trying to generate blank rows that contain empty cells. I cannot seem to identify where I am going wrong. What is the best possible solution
Updated version
<table class = "rota">
<thead>
<th>Date</th>
<% #hospitals.each do |hosp| %>
<th><%= hosp.name%></th>
<% end %>
</thead>
<tbody>
<%- if #rota_days.blank? %>
<tr>
<td colspan="<%= #hospitals.count %>">No rota day</td>
</tr>
<% end -%>
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
You're ending your loop too soon and you'll need a td for each th so change:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% end %>
</tr>
To:
<% #dates.each do |date| %>
<tr>
<td><%= date.inspect %></td>
<% (1..#hospitals.count).each do %>
<td></td>
<% end %>
</tr>
<% end %>

Rails For Loop View

I have the following code:
<tbody>
<%= Item.each do |item|=%>
<tr>
<th><%= item.rev =%></th> <=========
<th><%= item.name =%></th>
</tr>
<%= end =%>
</tbody>
However I am getting a syntax error on the inidcated line. There is data in the database(Test case). No idea what I am doing wrong.
The equals to signs you have are wrong. Try as below:
<tbody>
<% Item.each do |item|%>
<tr>
<th><%= item.rev %></th>
<th><%= item.name %></th>
</tr>
<% end %>
</tbody>
The <%= should only be used for expressions that need to be evaluated.
To help understand embedded ruby see this link
http://www.ruby-doc.org/docs/ProgrammingRuby/html/web.html
The expression for erb tags is <% #code %>
now if we want to print that tag too then we apply <%= #code %>
i.e. only one '=' sign is used and that too on left side.
Also in line each iterator there nothing can be printed, hence no '=' sign in
that line, similar is the case with tags containing 'end'.
Hence your code should look like
<tbody>
<% Item.each do |item| %>
<tr>
<th><%= item.rev %></th>
<th><%= item.name %></th>
</tr>
<% end %>
</tbody>