mycellius
mycellius

Reputation: 598

Rails 4 ToDo list, adding rows to the table

I am able to add tasks to my page, however only the first task is displayed in the table. The rest is added as regular, unformatted text.

Here is my home.html.erb

<%= content_for :title, "Home" %>
<h1>Things To Do</h1>

<div class="container" id="table-width">
<% if @tasks.empty? %>
<span>There are no tasks at the moment!</span>
<% else %>
  <table class="table">
    <thead>
      <td>Task</td>
      <td>Note</td>
      <td>Created At</td>
    </thead>
    <tbody>
      <% @tasks.each do |task| %>
      <tr>
      <td><%= task.title %></td>
      <td><%= task.note %></td>
      <td><%= task.created_at.strftime("%A,%B,%Y") %></td>
      </tr>
    </tbody>
  </table>
  <% end %>
<% end %>
<%= link_to "New Task", new_path, class: "btn btn-primary" %>
</div>

And here is my new.html.erb

 <h1>Write your task here</h1>

<div>
<%= form_for(@task) do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :note %>
<%= f.text_area :note, class:  "form-control" %>
</div>
</div>

<%= f.submit "Create Task", class: "btn btn-primary" %>
<% end %>

Upvotes: 0

Views: 72

Answers (1)

Luis Menjivar
Luis Menjivar

Reputation: 777

You start a loop at <% @tasks.each do |task| %> you should end the loop before you end the table, like so :

    <tbody>
      <% @tasks.each do |task| %>
        <tr>
          <td><%= task.title %></td>
          <td><%= task.note %></td>
          <td><%= task.created_at.strftime("%A,%B,%Y") %></td>
        </tr>
      <% end %> #this ends the @task.each do loop
    </tbody>
  </table>
<% end %> #this ends the if else conditional

Upvotes: 1

Related Questions