alkaloids
alkaloids

Reputation: 155

Why is my html getting mal-formed?

I have the following table in a form that's embedded in a formtastic form (semantic_form_for). Everything I ask to be generated by ruby shows up, but the table gets badly mangled (essentially, the tags NEVER get formed.

The table headers get drawn correctly

There are 14 available_date objects that get passed, and they alternate between having a time value of 1 or 2, so this is just terribly boggling, but probably simple to fix...

<table class="availability_table">
    <tr>
        <th>Date</th>
        <th>Early</th>
        <th>Late</th>
    </tr>
    <% f.fields_for :available_dates do |ad| %>
        <% if ad.object.time == 1  #if this is an early shift, then start the new row %>
            <tr><td><%= ad.object.date.strftime('%a, %b %d, %Y') %></td>
                <td><%= ad.collection_select(:availability , LookupAvailability.all.collect, :id, :name) %></td>
        <% else #otherwise end the row with just a box%>
            <td><%= ad.collection_select(:availability , LookupAvailability.all.collect, :id, :name) %></td></tr>
        <% end %>
    <% end %>
</table>

So like I said, the form functions properly, and the objects all get updated and displayed correctly and all that, it's just that the HTML isn't getting echo'd out properly so my table is all mangled. Help!

Upvotes: 0

Views: 80

Answers (3)

klew
klew

Reputation: 14967

Try this:

<% f.fields_for :available_dates do |ad| %>
  <tr>
    <% if ad.object.time == 1  #if this is an early shift, then start the new row %>
      <td><%= ad.object.date.strftime('%a, %b %d, %Y') %></td>
      <td><%= ad.collection_select(:availability , LookupAvailability.all.collect, :id, :name) %></td>
    <% else #otherwise end the row with just a box%>
      <td><%= ad.collection_select(:availability , LookupAvailability.all.collect, :id, :name) %></td>
      <td></td>
    <% end %>
  </tr>
<% end %>

I changed <tr> and </tr> positions and added <td></td> to add empty table cell.

Upvotes: 2

user307641
user307641

Reputation: 16

Are you using ruby 1.8.7?

If so, try to remove the comments next to the if and else lines. On erb, the only valid syntax for comments is <%# this is a comment %>. That is, the "#" should be at the beginning, right after the "<%", not after an instruction. On ruby 1.8.7, it crashes randomly, mangling the rendered html.

Upvotes: 0

alex.zherdev
alex.zherdev

Reputation: 24164

You close your tr tag only in the else branch. Would make sense to close it after the if-else.

Upvotes: 0

Related Questions