Hello! I'm pulling my hair out trying to get a simple view to display
correctly.
I need to display the practice info for each of the providers in my
database. Each provider has multiple practices. I would like the
second, third, fourth ... provider_id, location and provider_no to
line up under the first provider_id, location, etc.
My problem is that each additional practice is added as a new row,
aligned with the left margin, instead of under the appropriate header.
I don't know if this is an html issue, or a ror issue. Because I'm
dangerously newbie.
This is what the view looks like:
All Providers:
Title First Name Last Name License ID Location Provider Number
Dr Tom Close MD 4 RTH T01
Dr Evan Spam MD 5 RTH T02
Dr Ted Blue MD 6 RTH T10
6 RMC N07
Dr Jack Horner MD 7 RMC N09
7 RTH T10
Dr John Left MD 8 RMC N34
8 RTH T14
This is the view file [lookup.rhtml]:
<html>
</br>
<h1>All Providers:</h1>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>License</th>
<th>ID</th>
<th>Location</th>
<th>Provider Number</th>
</tr>
<% for provider in @providers %>
<tr>
<td><%= provider.title %></td>
<td><%= provider.first_name %></td>
<td><%= provider.last_name %></td>
<td><%= provider.lic_type %></td>
<% for practice in provider.practices %>
<td><%= practice.provider_id %></td>
<td><%= practice.location %></td>
<td><%= practice.provider_no %></td>
</tr>
<% end %>
<% end %>
</table>
<br />
Here are the models:
class Provider < ActiveRecord::Base
has_many :identifiers
has_many :practices
end
class Practice < ActiveRecord::Base
belongs_to :provider
end
Here is the related providers_controller def:
def lookup
@providers = Provider.find(:all, :include =>[:practices])
end
Any insight anyone could provide would be most welcome. Thank you!
i'm not sure if i understand exactly how you want to display this,
a bit of indentation shows at least one obvious error (i think)
<% for provider in @providers %>
<tr>
<td><%= provider.title %></td>
<td><%= provider.first_name %></td>
<td><%= provider.last_name %></td>
<td><%= provider.lic_type %></td>
<% for practice in provider.practices %>
<td><%= practice.provider_id %></td>
<td><%= practice.location %></td>
<td><%= practice.provider_no %></td>
</tr> <- should be outside loop
<% end %>
<% end %>
this would most likely work better:
<% for provider in @providers %>
<tr>
<td><%= provider.title %></td>
<td><%= provider.first_name %></td>
<td><%= provider.last_name %></td>
<td><%= provider.lic_type %></td>
<% for practice in provider.practices %>
<td><%= practice.provider_id %></td>
<td><%= practice.location %></td>
<td><%= practice.provider_no %></td>
<% end %>
</tr>
<% end %>
but it wouldn't work good, since it would leave the first cells empty
this would work better i think
<% @providers.each do |provider| %>
<tr>
<td><%= provider.title %></td>
<td><%= provider.first_name %></td>
<td><%= provider.last_name %></td>
<td><%= provider.lic_type %></td>
</tr>
<% provider.practices.each do |practice| %>
<tr>
<td>$nbsp</td> <td>$nbsp</td> <td>$nbsp</td> <td>$nbsp</td>
<td><%= practice.provider_id %></td>
<td><%= practice.location %></td>
<td><%= practice.provider_no %></td>
</tr>
<% end %>
<% end %>
but it would create a new row for every practice
which you could handle with a flag or something like
<% @providers.each do |provider| %>
<tr>
<td><%= provider.title %></td>
<td><%= provider.first_name %></td>
<td><%= provider.last_name %></td>
<td><%= provider.lic_type %></td>
<% provider.practices.each_with_index do |practice, index| %>
<% if index == 0 then %>
<tr>
<% else %>
<tr>
<td>$nbsp</td> <td>$nbsp</td> <td>$nbsp</td> <td>$nbsp</td>
<% end %>
<td><%= practice.provider_id %></td>
<td><%= practice.location %></td>
<td><%= practice.provider_no %></td>
</tr>
<% end %>
<% end %>
something on that line should work
there are a few nice effects you can get with the colspan html attribute
in cases like that