capcode01
capcode01

Reputation: 163

div_for within a table

I am trying to create a table that populates based on all of the records for a specific model.

Here is the index.html.erb file

    <%= div_for(@departments, :class => "test") do |department| %>
            <tr>
                <td class="indexc1">
                    <%= department.name %>
                </td>
                <td class="indexc2">
                    <%= department.location %>
                </td>
                <td class="indexc3">
                    <%= department.date_completed %>
                </td>
                <td class="indexc4">
                    <ul class="action">
                        <li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
                        <li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
                        <li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
                    </ul>
                </td>
            </tr>
    <% end %>

This seems to properly create the table rows but does not put them into divs as expected.

I would like to be able to create one table row for each department and then be able to use AJAX to add or remove them.

Upvotes: 1

Views: 312

Answers (1)

Scheuk
Scheuk

Reputation: 36

If you want each row to be wrapped in a div, you'll need to add the div class and id to the tr tag, IE: (tr id="myid" class="class") rather than wrapping the div around the tr.

You can use the rails content_tag_for to generate this:

<% @departments.each do |department| %>
        <%= content_tag_for(:tr, department, :class => "test") do %>
            <td class="indexc1">
                <%= department.name %>
            </td>
            <td class="indexc2">
                <%= department.location %>
            </td>
            <td class="indexc3">
                <%= department.date_completed %>
            </td>
            <td class="indexc4">
                <ul class="action">
                    <li><%= link_to 'Edit Info', edit_department_path(department), :class=>"tlink3" %></li>
                    <li><%= link_to 'Edit Tasks', department_path(department), :class=>"tlink3" %></li>
                    <li><%= link_to 'Destroy', department, method: :delete, data: { confirm: 'Are you sure?' }, :class=>"tlink3" %></li>
                </ul>
            </td>
        <% end %>
<% end %>

Upvotes: 2

Related Questions