Reputation: 31
I am new in ruby on rails and I am creating my first ruby on rails project. I created a html view with embedded ruby codes that will display a table with the following headings: Patient, Room/Bed, Covering OT/PT. Under those headings, the patient name, corresponding room/bed and OT/PT will be displayed. But when the codes run, it do not render the patient name, room/bed and covering OT/PT. The table headings render but not the patient name, room/bed, and covering OT/PT. Any codes within this ruby block <% @units.each do |un| %> ... <%end%> apparently are not executed even if I put a ruby code like <%= Mr. Jones %>. I am not sure what to do. Any help and advice is greatly appreciated. Here is the html code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Project</title>
<meta name="description" content="Project1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="shortcut icon" href="/favicon.ico"> -->
<meta name="author" content="David West">
<link rel="icon" type="img/ico" href="/assets/images/jhu_tic.ico">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400">
<link rel="stylesheet" href="/css/enterprise-auth.min.css">
<div class="bar-header">
<div class=label>
<div class=app-label>Project | </div>
<div class=view-label>Therapist</div>
</div>
<div class="date">Today is <%= Time.now.to_date %></div>
</div>
</div>
<div class="main-page">
<% @units.each do |un| %>
<div class="patient-queue-wrapper">
<div class="queue-header">Daily Tx and other patients with OT/PT lag 2+ days</div>
<table class="mdl-data-table mdl-js-data-table mdl-shadow--2dp fixed-table-header">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">Patient Name</th>
<th class="mdl-data-table__cell--non-numeric">Room/Bed</th>
<th>OT/PT Lag</th>
<th>OT/PT AMPAC</th>
<th class="mdl-data-table__cell--non-numeric">Covering OT/PT</th>
</tr>
</thead>
<tbody class="table-body scrollable-body">
<% un.patients.each do |patient| %>
<% if patient.lag_time_approaching_thresh %>
<tr>
<td class="mdl-data-table__cell--non-numeric"><%= patient.name %></td>
<td class="mdl-data-table__cell--non-numeric"><%= patient.room_bed %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
<%end%>
</div>
</head>
</html>
Here is the codes showing the @unit variable inside the DashboardsController:
class DashboardsController < ApplicationController
def therapist
@patients = Patient.all.includes(:pt_priority, :ot_priority, orders: [visi$]
@units = Unit.all
end
def therapist_all_units
@units = Unit.all
end
def therapist_unit
@unit = Unit.find(params[:id])
# use the link-to helper methods
end
def manager
end
Upvotes: 1
Views: 433
Reputation: 12847
The code inside the tbody block will not execute if un.patients returns empty? or nil This will be why nothing you place inside that block will display
If you place an simple html tag inside that block it won't appear e.g.
<tbody class="table-body scrollable-body">
<% un.patients.each do |patient| %>
<h1> If un.patients is then nothing in here will display </h1>
<% if patient.lag_time_approaching_thresh %>
<tr>
<td class="mdl-data-table__cell--non-numeric"><%= patient.name %></td>
<td class="mdl-data-table__cell--non-numeric"><%= patient.room_bed %></td>
</tr>
<% end %>
<% end %>
</tbody>
This is because there is no data in un.patients. Now then, why there is no data is an entirely different question. Check your association in the console run from the command line inside the root folder of your application.
$ rails c
You can use the console to interact with your models to interrogate your data and your model structures
e.g.
Patient.first.unit
you may find that the above returns nil when you are expecting a unit to return
I suggest you look very closely at your data and in particular the relationships between unit and patient and you will probably find that there is nothing wrong with your form
You could also add a check in the html form for this condition
<% if un.patients.empty? %>
<h2> Sorry, there are no patients for this unit </h2>
<%else%>
<tbody class="table-body scrollable-body">
<% un.patients.each do |patient| %>
<% if patient.lag_time_approaching_thresh %>
<tr>
<td class="mdl-data-table__cell--non-numeric"><%= patient.name %></td>
<td class="mdl-data-table__cell--non-numeric"><%= patient.room_bed %></td>
</tr>
<% end %>
<% end %>
</tbody>
<%end%>
You might want to sort out the code indentation to make it more readable
Upvotes: 1