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
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
Related
I have a table that stores all billing history for each user. I need to display certain fields from this table in an invoice for the user. When I call the variables in my view I get a 'undefined method error'.
Here is a small example of what i have in the view.
<tbody>
<tr>
<td>
Invoice Number
</td>
<td>
<%= #billing_history.id %>
</td>
</tr>
<tr>
<td>
Issue Date
</td>
<td>
<%= #billing_history.created_at.strftime('%d/%m/%Y' ) %>
</td>
</tr>
<tr>
<td>Tax Number</td>
<td>
######
</td>
</tr>
</tbody>
Here is my controller
def invoice
#company = current_user.company
#billing_history = BillingHistory.find_by(id: params[:billing_id])
# raise 'not for you' if #billing_history.company != #company.id
render pdf: 'billings/invoice'
end
Here is my full error message
> `ActionView::Template::Error (undefined method `id' for nil:NilClass):
4: <meta charset="UTF-8" />
5: <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6: <meta http-equiv="X-UA-Compatible" content="ie=edge" />
7: <title>Invoice <%= #billing_history.id %></title>
8: <style>
`
try using byebug place it after
#billing_history = BillingHistory.find_by(id: params[:billing_id]) this line
then check what is the value of #billing_history if it's nil then use The Safe Navigation Operator (&.) like #billing_history&.id ,that won't raise any error.
I've been trying to use my customized content for the Jenkins build emails. I got stuck when I am trying to change the contents of html dynamically based on the results from previous stages. I am passing the contents of an html to a variable as below and when i try to access the hash list in that, the value of arrays(key and value of an array) are not getting printed.
def content = """\
<html>
<head>
<style>
</style>
</head>
<body>
<table style=" background-color:lightgreen; width:900px;margin:0;">
<tr>
<th>Build Results :</th>
</tr>
<tr>
<td>project Name : </td>
<td>project URL : </td>
</tr>
<tr>
<td>Build Number : ${var3} </td>
</tr>
<tr>
<td>Buid URL :</td>
</tr>
</table>
<table style=" background-color:lightblue; width:900px;margin:0;">
<tr>
<% stagearray.each { item ->
def key=item.key;
def value=item.value; %>
<td>${key}</td>
<td>${value}</td>
<% } %>
</tr>
this is the sample
<tr><td>This is test line </td></tr>
</table>
</body>
</html>
"""
Any help would be highly appreciated.
I have a wired problem when developing rails. The order of elements in container is
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
For most of the pages, it works well. But it goes wrong when it renders following page
<% provide(:title, 'All Apps') %>
<h1>All Apps</h1>
<table class="apps table">
<thead>
<tr>
<th>App Name</th>
<th>Status</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<%= render #apps %>
</tbody>
</talbe>
The order of elements in container becomes
<div class="container">
<h1>All Apps</h1>
<footer class="footer">...</footer>
<pre class="debug_dump">...</pre>
<table class="app table">...</table>
</div>
What confuses me is that only this page goes wrong. I am using Rails 4.0.5.
What is most probably happening is your browser HTML parser is trying to fix things, by putting elements "floating" in a table (without being in a tr>td) before the table. You probably have an unclosed tag.
(which is why it's vering important in cases like these to check the generated html and compare it to the browser's DOM panel - it can be very different !)
I have a "search" page that have some controls and below is the search page code:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
</table>
</td>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">
Result
</h2>
<% #user_zip.each do |uzr_zip| %>
<h1><%= uzr_zip.First_Name %></h1>
<% end %>
<table id="searchResult" width="100%" runat="server">
<tr>
<td bgcolor="#CCDBE0">
Image:
</td>
<td bgcolor="#CCDBE0">
<%= f.label(:zip, "Mentor") %>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% end %>
And when I am trying to get the textbox value into the controllers page like below
def search
#students=Students.all
#blah = params[:search][:tf_Zip]
end
render 'search'
end
Then it gave me an error below, at this line #blah = params[:search][:tf_Zip]
undefined method `[]' for nil:NilClass
Kindle help me. Thanks
I think your params[:search] is nil?
so for this you can use
#blah = params[:tf_Zip]
or change your input field like this
<%= f.text_field :tf_Zip %>
or you can use like this
<input type="text" name="search[tf_Zip]" Style="text-align: left;"
BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.
The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.
Your problem is this line:
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].
What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
can be replaced with
<%= f.text_field :tf_Zip %>
which will populate params[:search][:tf_Zip]
For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.
Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.
Usually you'd submit the form from one controller action and handle the result in another action.
And your statement end looks misplaced.
I'm using a Bootstrap modal. Right now the table listed in the modal gets loaded when the whole html page loads. I would like the data for the table loaded when the modal launches.
How can I load the data in the modal when the modal is launched?
This is the first part of my modal - it contains several Bootstrap tabs.
<div class="modal-body">
<div class="tabbable">
<ul class="nav-tabs">
<li class="active">Details</li>
<li>Materials</li>
<li>Labor</li>
<li>Tasks</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1_<%= workorder.id %>">
<table border="1" cellpadding="5">
<tr>
<th>Description</th>
<td><%= workorder.description %></td>
</tr>
<tr>
<th>Client</th>
<td><%= workorder.client.client_name %></td>
</tr>
<tr>
<th>Type</th>
<td><%= workorder.type.typecode %></td>
</tr>
<tr>
<th>Priority</th>
<td><%= workorder.wopriority.prioritycode %></td>
</tr>
<tr>
<th>Scheduled Finish</th>
<% if workorder.scheduled_finish != nil %>
<td>Scheduled Finish = <%= workorder.scheduled_finish %></td>
<% else %>
<td></td>
<% end %>
</tr>
</table>
</div>
...
Thanks for the help!
Use Ajax (set your links to link_to with remote => true) and render a partial. Then set the innerHTML of tab-pane to the partial content returned via Ajax. Of course you'll need to add logic in your controller to parse out only the data for each tab.
Might help to read up on:
http://guides.rubyonrails.org/ajax_on_rails.html
You have two choices:
Using AJAX to populate the table, when your mondal is launched
Mondal with an iframe
If you're just simply displaying information, then an iframe is a quick and dirty solution.