Selecting individual documents/objects from MongoDB collection that's in a loop? - html

I'm not sure how to word this exactly...but basically in my code I am looping through my MongoDB collection using forEach and displaying the objects within them with ejs in a table. I want to make it so that when a user clicks on one of those objects, it takes them to a page with details for just the item they selected, but can't do that because they are displayed via a loop.
For example:
My collection, called stores, has objects like environment, store, and version. When I display them with a loop, this is how I do it:
<table>
<tr>
<th>Environment</th>
<th>Store Name</th>
<th>Code Version</th>
</tr>
<% stores.forEach(function(store){ %>
<tr>
<td> <%= store.environment %> </td>
<td> <%= store.store %> </td>
<td> <%= store.version %> </td>
</tr>
<% }); %>
</table>
and the resulting table looks something like this:
Environment | Store Name | Code Version
-----------------------------------------
QA1 | Oakdale | 2019.09
QA2 | Westminster | 2020.03
QA3 | Garden Grove | 2020.05
I want the user to be able to click on, for example, QA1 and it will direct them to another page for details on QA1. My question is, with the code being displayed as a group via <%= store.environment %>, how do I create a link on JUST QA1, or just QA2? Right now if I put a link in the same td as <%= store.environment %>, that link shows up in every instance of environment.
Is there maybe a way through jQuery or something to select the specific environments?

Yes, you can, you'll just have to check the environment property, or whatever property you're using to define if should show the link.
Here's a quick example of how you could do that:
<table>
<tr>
<th>Environment</th>
<th>Store Name</th>
<th>Code Version</th>
</tr>
<% stores.forEach(function(store){ %>
<tr>
<td>
<% if (store.environment === "QA1") { %>
<%= store.environment %>
<% } else { %>
<%= store.environment %>
<% } %>
</td>
<td> <%= store.store %> </td>
<td> <%= store.version %> </td>
</tr>
<% }); %>
</table>
Sorry for any code mistake, I'm doing direct from StackOverflow.

Related

Ruby on Rails - Creating rows in an html.erb table by looping over instance variable

I have an instance variable #referrals which contains names of referrals a person has done. I now need to create a table showing all these referrals as rows. I have tried the following code in the html.erb file:
<table style="border:1px solid black;margin-left:auto;margin-right:auto;">
<th> Referral Emails </th>
<% #referrals.each do |referrals| %>
<tr> referrals </tr>
<%end %>
</table>
and
<table style="border:1px solid black;margin-left:auto;margin-right:auto;">
<th> Referral Emails </th>
<% #referrals.each do |referrals| %>
<%= <tr> referrals </tr> %>
<%end %>
</table>
Both show up errors.
I am new with Ruby, help with the correct Ruby code is much appreciated. Thanks a ton in advance.
Edits: Code changes as suggested by #mu is too short.
By default in ERB, code blocks are enclosed in <% and %> delimiters, and if you want the result of the code block run, you use the = symbol inside, like <%= 'Hel' + 'lo' %>, world would output "Hello, world".
Each member being iterated over with .each is named between the pipe symbols, so that is what you reference inside the block.
Also table rows need to have table data, or <td> tags within the row <tr> tags.
Here's what should work for you assuming that #referrals is a list of strings:
<table style="border:1px solid black;margin-left:auto;margin-right:auto;">
<tr> <th> Referral Emails </th> </tr>
<% #referrals.each do |referral| %>
<tr> <td> <%= referral %> </td> </tr>
<% end %>
</table>

How to get first data from the array?

I have an issue in the node (html file). I need to get the first data instead of everything. I used this
<% if(tblData) {%>
<% tblData.forEach(function(res,row) {%>
<tr>
<td>
<%= res.name %>
</td>
</tr>
<% }) %>
<% } %>
I got all the names. But instead of getting all the names I want only the first name. Is there any solution for this?
You can get the first element of a table by putting its index in square brackets (myTable[0]):
<% if(tblData) {%>
<tr>
<td>
<%= tblData[0].name %>
</td>
</tr>
<% } %>

Print object only when conditions met

I have three objects and two associations. My objects are Owner, Jockey, and Horse, and the associations are OwnerID (Owner) -> OwnerID (Jockey) and JockeyID (Jockey) -> JockeyID (Horse).
On the page for my Owner, I would like to print all Horses. I did this like so:
<table>
<tr>
<th>Name</th>
<th>JockeyID</th>
</tr>
<% #subject.FirstAssociation.SecondAssociation.each do |horse| %>
<tr>
<td>
<%= horse['Name'].first %>
</td>
<td>
<%= horse['JockeyID'].first %>
</td>
</tr>
<% end %>
</table>
This is close to what I want: it prints out all of the horses owned an Owner, but I would like to display, following these associations (so for this Owner) all of the Horses which have a JockeyID = 7.
How can I do this?
try this
<% #subject.FirstAssociation.find(7/jokeyid).SecondAssociation.each do |horse| %>

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

Two Column HTML table using Rails 3

I seriously searched for 3 days on this topic, but I am unable to find anything that can help me solve my issue.
I have a table that prints out Kanban cards for the factory I work in, but the Avery sheet where I need to print them is 2 col by 3 rows, and my table is displaying 3 col X 2 rows.
card 1|card 2|card 3
card 4|card 5|card 6
I need this layout to be 2 col by X rows.
card 1|card 2
card 3|card 4
card 5|card 6
This is my Rails view:
<% count = #card.start%>
<% #card.finish.times do %>
<table border="0" cellspacing="5" cellpadding="5" class="table">
<tr>
<tr>
<td id="part_no_card_no" class="all">
<%= #card.part_no %>
&nbsp
&nbsp
<%= count %> /
<%= #card.finish %>
</td>
</tr>
<tr>
<td>
<%= image_tag "#{#card.part_no}" + "-" + "#{count}" + "-" + "#{#card.finish}" + ".png" %>
</td>
</tr>
<tr>
<td>
<%= #card.description %>
</td>
</tr>
<tr>
<td>
<%= #card.from_loc %>
-
<%= #card.to_loc %>
</td>
</tr>
<tr>
<td>
Cantidad en Bin: <%= #card.bin_qty %> EA
</td>
</tr>
</td>
</tr>
</tr>
</table>
<% count += 1 %>
<% end %>
I hope someone could provide some direction on this task.
First off, I'm not quite understand about your card system and it's attribute, since you just show some simple layout but your view looks more advance about it. So I give you an example about my code, and I will edit whenever it's needed.
I have a table named user_menus and I want to show it like: ([] mean checkbox)
[] Products [] Suppliers
[] Categories [] Units
I use .in_groups_of() for it.
<table class="table table-condensed">
<% #user_menus.in_groups_of(2, false) do |user_menu_array| %>
<tr>
<% for user_menu in user_menu_array %>
<td>
<label class="checkbox">
<%= check_box_tag "user_group[user_menu_ids][]", user_menu.id, #user_group.user_menus.include?(user_menu), :checked => true %>
<%= user_menu.name %>
</label>
</td>
<% end %>
</tr>
<% end %>
</table>
With this code, I can produced that easily.
this is more of a HTML thing. To have two columns in a row, keep two 's inside a tag eg.
e.g for 2 columns
<tr>
<td>col-1</td>
<td>col-2</td>
</tr>
for 4 columns
<tr>
<td>col-1</td>
<td>col-2</td>
<td>col-3</td>
<td>col-4</td>
</tr>