I have a table which has price and quantity fields. I want to add the price * quantity to the grand total for each item that i eventually add to the table.
My code looks like this.
<table>
<thead>
<tr>
<th width="200">Name</th>
<th width="150">Price</th>
<th>Quantity</th>
<th width="150">Total</th>
</tr>
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td class="actions">
<% link_to("update", :class => 'action update') %>
<% link_to("X", :class => 'action delete') %>
</td>
</tr>
<% end %>
</thead>
</table>
and my grand total is in the form of a label.
how do i do this? Is there static variable concept in RoR??
You should add the field grand_total in the table and create a callback in the Item model. This callback will save the value of grand total in the table each time a new item is created.
before_save : save_grand_total
def save_grand_total
self.grand_total = self.quantity * self.price
end
You could use Enumerable#inject to get the grand total as follows:
<% #= #item.inject{ |grand_total, quantity| grand_total + (quantity * cart.price) } %>
Update:
Please ignore the above line of code, that was to show you an example. The following code example should solve your issue.
Place the following line where you'd like to display the grand total.
<%= label_tag 'grand_total',
#item.inject(0) { |grand_total, item| grand_total + (item.quantity * item.price) } %>
#item.inject(0){ |grand_total, item| grand_total + (item.quantity * item.price) } applies the block to each item. The first parameter grand_total in this case is first assigned initial value of 0. This is done through inject(0).
The block then starts accumulating (item.quantity * item.price) into grand_total which is the final value that is returned by the inject.
Hope this makes sense.
<table>
<thead>
<tr>
<th width="200">Name</th>
<th width="150">Price</th>
<th>Quantity</th>
<th width="150">Total</th>
</tr>
<% grand_total=0 %>
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<% grand_total+= item.quantity * item.price %>
<td class="actions">
<% link_to("update", :class => 'action update') %>
<% link_to("X", :class => 'action delete') %>
</td>
</tr>
<% end %>
</thead>
</table>
Related
I've got a form with nested attributes, now I'm wanting to show on a table, but I'm not able to break the line it gets interacting as a column. How can I do? What can I do?
Here's an image of my current table.
<% #salmonella.process_salmonellas.each do |process| %>
<tr align="center">
<td><%= process.title %></td>
<table>
<% process.stages.each do |stage| %>
<tr>
<td><%= stage.title %></td>
<td>
<table>
<% stage.lines.each do |line| %>
<tr>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
</tr>
<% end %>
</table>
</td>
</tr>
<% end %>
</table>
</tr>
And this is how it should look like
That would be more or less the table template I drew in my head:
<table>
<!-- Each for principal object -->
<tr>
<td><!-- fields of principal object --></td>
<td>
<!-- each nested attributes from principal object and the model -->
<tr>
<td><!-- fields of second object--></td>
<td>
<!-- each from model and another model that is nested too from principal object and model -->
<tr>
<td><!-- fields of third object --></td>
</tr>
</td>
</tr>
</td>
</tr>
</table>
that's how nested this is in my controller:
def salmonella_params
params.require(:salmonella).permit(:title,
process_salmonellas_attributes: [
:id,
:title,
:_destroy,
stages_attributes: [
:id,
:title,
:_destroy,
lines_attributes:[
:id,
:material,
:indicator_name,
:_destroy
]
]
])
end
I did as Jeremy recommended and it looked like this:
<% #salmonella.process_salmonellas.each do |process| %>
<tr>
<% process.stages.each do |cont| $b = cont.lines.count end %>
<td rowspan="<%= $b %>"><%= process.title %></td>
<% process.stages.each do |stage| %>
<td rowspan="<%= stage.lines.count %>"><%= stage.title %></td>
<% stage.lines.each_with_index do |line, o| %>
<% if o == 0 %>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
<% else %>
<tr>
<td><%= line.material %></td>
<td><%= line.indicator_name %></td>
</tr>
<% end %>
<% end %>
</tr>
<% end %>
Now it's working the way the image works.
My table is showing two columns, "#" and "String". I have made the table with each td having its value shown through a text_field so the user can change the value and submit the changes.
The problem is how to send all the values with the form since each td has the same id and name making only the last td sent.
How do I send all the td values?
The table:
<%= form_for(#skein) do |s| %>
<table width="100%" class="tablesorter">
<thead>
<tr>
<th width="20%" data-placeholder="Search">#</th>
<th width="80%" data-placeholder="Search">String</th>
</tr>
</thead>
<tbody>
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td><%= s.text_field :string_id_bk, :value => number %></td>
<td><%= s.text_field :string_id, :value => string %></td>
</tr>
<% end %>
</tbody>
</table>
<%= s.submit "Save String File", id: "commit", class: "btn btn-lg btn-primary" %>
HTML td rendered:
<td><input value="Ende"" type="text" name="skein[string_id]" id="skein_string_id"></td>
Notice how the name and id will be the same, causing the data to overwrite the data sending just the last data.
The table has thousands of records. I cannot send each td as an attribute parameter, a parameter should be a long array.
Try this
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td> <%= text_field_tag 'string_id_bk[]', number %> </td>
<td> <%= text_field_tag 'string_id []', string %> </td>
</tr>
<% end %>
Give unique id for the text fields as shown below
<% #split_order_content.zip(#split_content).each do |number, string| %>
<tr>
<td><%= s.text_field :string_id_bk, :value => number ,:input_html => { :id => '#{s.id}'+'bk' } %></td>
<td><%= s.text_field :string_id, :value => string ,:input_html => { :id => s.id }%></td>
</tr>
<% end %>
Hope that will help you
I want to display only a single row in the table. My controller for the same looks like this:
def Add
#item= Item.find(params[:id])
end
and my table row looks like this.. Isn't there a substitute for the method .each to just display one record?
<% #item.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.price %></td>
<td><%= item.quantity %></td>
<td><%= item.quantity * item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
<% end %>
In this case you don't need loop. U can directly create table like this
<tr>
<td><%= #item.name %></td>
<td><%= #item.price %></td>
<td><%= #item.quantity %></td>
<td><%= #item.quantity * #item.price %></td>
<td>
<% link_to("update", '#', :class => 'action update') %>
<% link_to("X", '#', :class => 'action delete') %>
</td>
</tr>
#item= Item.find(params[:id])
Here, #item will be an object of class Item. So You don't need to Iterate the Object with 'each'. You can directly access the Item attribute by #item object.
#item.name
#item.price
#item.quantity.....
I have a mysql table with product codes, and some products have different colours i.e. red, blue ect. which have different codes so a red pen would be REDPEN and the blue one would be BLUEPEN but in another column in my table they will have the same code to group them by i.e Colouredpen
When clicked on the pen to view the details i want to show all the different colours for that pen. How do i do this using rails?
My current code:
product_controller
class ProductController < ApplicationController
def index
end
def prod
#products = Product.uniq.pluck(:index_code)
end
end
view
<% #products.each do |product| %>
<tr class="mouse">
<td><%= product.product %></td>
<td>
<% if product.stock < 1 %>
<p class="outstock">Out of Stock</p>
<% else %>
<p class="instock">In Stock</p>
<% end %>
</td>
<td><%= product.size + ' - ' + product.colour %></td>
<td>£<%= product.price %></td>
<td><%= text_field_tag :Quantity, 1, :size => 2, :class => "qtybox" %></td>
<td><%= image_tag("buy.png", :alt => "buy") %></td>
</tr>
<tr>
<td colspan="6">
<table class="divider" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style="height: 1px;"></td>
</tr>
</table>
</td>
</tr>
<% end %>
To get the distinct codes, you can write :
Pen.select(:code).map(&:code).uniq
or
Pen.uniq.pluck(:code)
I'd like to view data from different database-tables in a view with tables like this picture shows:
I'm familiar with HTML tags <table>, <td> and <tr>, but I'm having trouble with multiple queries in a column.
<table>
<tr>
<th>Skills </th>
<th>Expected-qualifications</th>
<th>Current-qualifications</th>
</tr>
<% #employee.position.skills.each do |skill| %><% #employee.position.expected_qualifications.each do |expected_qualification| %><% #employee.current_qualifications.each do |current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %><% end %><% end %>
</table>
This code looks like this:
As you can see, the skills, expected-qualifications, and current-qualifications repeat.
My question: How should the codes be ordered in the table so it will look the way I want it to?
Try zip:
<% #employee.position.skills.zip(#employee.position.expected_qualifications,#employee.current_qualifications).each |skill expected_qualification current_qualification| %>
<tr>
<td><%= skill.kategorien %></td>
<td><%= expected_qualification.sollqualifikation %></td>
<td><%= current_qualification.istqualifikation %></td>
</tr>
<% end %>
if there is REALLY can be more than one skill, expected_qualification and current_qualification so you use has_many assosiation forposition
<tr>
<td><%= #employee.position.skills.map(&:kategorien).join(", ") %></td>
<td><%= #employee.position.expected_qualifications.map(&:sollqualifikation).join(", ") %></td>
<td><%= #employee.current_qualifications.map(&:istqualifikation).join(", ") %></td>
</tr>
Otherwise you should use has_one association