Rails 3 - tables in html.erb - html

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

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| %>

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.

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>

Removing columns from a table

I'm following a tutorial on how to create a Ruby-on-Rails blogging website with comments and tags, and have put my work so far on https://github.com/khpeek/jumpstart-blogger/.
The last part of the tutorial involves allowing authors to create user names and passwords. One of the pages is a default listing of the authors from the "sorcery" gem:
As you can see, there are blank columns with "Password" and "Password confirmation", which I'd like to remove.
The appearance of this page is governed by app/views/authors/index.html.erb, which reads
<p id="notice"><%= notice %></p>
<h1>Listing Authors</h1>
<table>
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Password confirmation</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #authors.each do |author| %>
<tr>
<td><%= author.username %></td>
<td><%= author.email %></td>
<td><%= author.password %></td>
<td><%= author.password_confirmation %></td>
<td><%= link_to 'Show', author %></td>
<td><%= link_to 'Edit', edit_author_path(author) %></td>
<td><%= link_to 'Destroy', author, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Author', new_author_path %>
My thought was to comment out the lines
<th>Password</th>
<th>Password confirmation</th>
and
<td><%= author.password %></td>
<td><%= author.password_confirmation %></td>
However, if I do this, some text gets placed outside the main bounding box:
Is it possible to tell from this limited portion of the code what is going wrong here?
It happens because you have no style to "tell" your table to fill the full width.
Try adding the following stylesheet:
<style>
table { width: 100%; }
</style>

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 %>