Each nested attributes in a table in Rails - html

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.

Related

Unexpected list of items on #index

On my index page, I succeeded in listing all of my 'games' in a table, unfortunately, another list, that isn't part of my index.html.erb file code also appears, above my table.
I don't understand how this is possible as my html file doesn't contain any element at the place the list appears on the browser... If someone has an idea that would be very nice !
Here's a photo of what appears on the browser :
browser problem snapshot
Here's my index.html.erb code :
<div class="container full-height">
<div class="abs-center">
<table class="table">
<thead>
<tr>
<td colspan="6">GAMES</td>
</tr>
</thead>
<tbody>
<%= #games.reverse.each do |g| %>
<tr>
<td><%= g.id %></td>
<td><%= g.score_1 %></td>
<td><%= g.score_2 %></td>
<td><%= g.created_at %></td>
<td><%= link_to 'see game', game_path(g) %></td>
<td><%= link_to 'modify', edit_game_path(g) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
My game controller index method :
def index
#games = Game.all
end
Thanks a lot !
Remove = before <%= #games.reverse.each do |g| %>. = renders a result of an expression, in your case, it is each method, that returns the collection.
Must look as this <% #games.reverse.each do |g| %>

Equal distribution for table rows - HTML AND CSS

I just made a table and I have three records that I'm trying to display. It successfully displays the information but it crams it all in one row. When I want it to display over three rows. I'm doing something obvious wrong but I still need some help. Here is my code:
HTML
<table>
<tr>
<th>Name</th>
<th>Manage</th>
</tr>
<tr>
<% #assignments.each do |assignment| %>
<td><%= link_to assignment.name, account_assignment_path(assignment) %></td>
<td><%= link_to "Delete", account_assignment_path(assignment), method: :delete %></td>
<% end %>
</tr>
</table>
Screen Shot:
As you can see they all try to fit in one column. How can I fix this so they spread out?
I'm not familiar with the templating language you're using, but this is likely due to not including the <tr> element in your loop.
<table>
<tr>
<th>Name</th>
<th>Manage</th>
</tr>
<% #assignments.each do |assignment| %>
<tr>
<td><%= link_to assignment.name, account_assignment_path(assignment) %></td>
<td><%= link_to "Delete", account_assignment_path(assignment), method: :delete %></td>
</tr>
<% end %>
</table>

How do I use Ruby in an HTML document

I have the following HTML in my index file.
<% #posts.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
I have a function called is_eligible which takes post.team_name as an argument. I want to run it each "do iteration" and skip all three printing steps if it returns false
I'm not sure how to
include the file is_eligible is in and
structure the code within the html somewhere.
Any ideas?
# The view
<% #posts.each_with_index do |post, index| %>
<% unless post.is_eligible? %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>
<% end %>
# The model post.rb
def is_eligible?
# and there you use self.name to access the name
end
<% #posts.select{|p| is_eligible(p. team_name)}.each_with_index do |post, index| %>
<tr>
<td><%= index+1 %></td>
<td><%= post.team_elo %></td>
<td><%= post.team_name %></td>
</tr>
<% end %>

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)

Rails 3 - tables in html.erb

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