Send html table data has same name - html

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

Related

Add a new table row to existing table but before the first row

i am trying to make a transaction details page, how can i make the new input table row on the transactions table sort before the previous one? I use node, mongo, mongoose and javascript and ejs
<section id="transactions">
<div class="mx-5 my-5">
<table>
<tr>
<th>Transaction ID</th>
<th>Beneficiary Details</th>
<th>Timestamp</th>
<th>More</th>
</tr>
<% transactions.forEach(function(transaction) { %>
<tr>
<td><%= transaction._id %></td>
<td> <%= transaction.beneficiaryName %> <%= transaction.beneficiaryBank %> <%= transaction.beneficiaryAccountNumber %> <%= transaction.transferAmount %></td>
<td><%= transaction.time %></td>
<td></td>
</tr>
<% }) %>
</table>
</div>
If you are generating the list from transactions, and I guess it's an array of objects, you can use:
transactions.unshift({<newObject>})
to add it to the beginning of the array
And if you want to do it with JQuery, use:
$('table > tbody > tr:first').before('<tr><td>Stuff</td></tr>');

Each nested attributes in a table in Rails

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.

Merge tables to a single table

Relevant snippets
Show.html.erb
<% outbound_messages.each do |outbound_message| %>
<h5>Outbound Message</h5>
<%= render "trace/display_tabular_data", :data => outbound_message %>
<% end %>
Display_tabular_data.html.erb
<table border="1px solid black">
<thead>
<tr>
<%data.each do |key,value|%>
<th><%=key.capitalize%></th>
<%end%>
</tr></thead><tr>
<%data.each do |key,value|%>
<td><%=value%></td>
<%end%>
</tr>
</table>
So what happens is that each row of data, gets printed in a unique table.
So one has something like http://imgur.com/1gskRvX
But clearly a much better result would be as a single table (Desired outcome)
Outbound Message
Message ID, Exchange, Relayed
Row1
Row2
Row3
Row4
...
....
Any ideas how I can go about this? Display_tabular_data is called at-least 15 times in different places in show.html.erb, so it would be much easier if it was somehow possible to get this final result by making changes only in display_tabular_data, and not in show.html.erb. If not possible, please give me the best possible way?
If you don't want to render a separate table for every object, how about something like this in show.html.erb:
<% unless outbound_messages.empty? %>
<%= render 'trace/display_tabular_data', :data => outbound_messages %>
<% end %>
And then in the partial:
<h5>Outbound Messages</h5>
<table border="1px solid black">
<thead>
<tr>
<% data.first.each do |key,value| %>
<th><%=key.capitalize%></th>
<% end %>
</tr>
</thead>
<% data.each do |outbound_message| %>
<tr>
<% outbound_message.each do |key,value|%>
<td><%=value%></td>
<% end %>
</tr>
</table>
This only works if you are confident that every outbound_message has the same set of keys.
Here you go..
<% if outbound_messages.count > 0 %>
<h5>Outbound Message</h5>
<table border="1px solid black">
<thead>
<tr>
<td>Message ID</td>
<td>Exchange</td>
<td>Relayed</td>
</tr>
</thead>
<% outbound_messages.each do |outbound_message| %>
<tr>
<td>
<%= outbound_message[:message_id] %>
</td>
<td>
<%= outbound_message[:exchange] %>
</td>
<td>
<%= outbound_message[:relayed] %>
</td>
</tr>
<% end %>
</table>
<% end %>
You can eliminate the partial entirely

Rails - How do I show products with a certain code in rails 3

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>&pound<%= 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)

How do I create a dynamic class for a div variable?

I have the following code in an ERB file:
<table border="1">
<% #lists.each do |list| %>
<tr class="even">
<td><%= link_to list.title, list %></td>
<td><%= link_to 'Edit', edit_list_path(list) %></td>
<td><%= button_to "Destroy", list_path(list), :method => :delete %></td>
</tr>
<% end %>
</table>
I want to make the <tr class="even" line dynamic. Each tr should get the class either "even" or "odd" depending on a counter variable that gets incremented every time my loop starts over. However, I cannot figure out the best way to implement this.
Use this nice helper :)
<tr class="<%= cycle("even", "odd") %>">