1
votes

I'm very new to Ruby and Rails so someone please help me with this..

I'm creating basically a To Do list app, and on my table I have a "Done" column. When I generated the scaffold in rails, I made the column like "done:boolean". So now when you create or edit a task it has a checkbox that you can either check or uncheck for "Done". So on the page where it displays the whole list of tasks, in the "Done" column it states either "True" if it was checked, or "False" if it was unchecked. So my question is, How do I replace that "True" or "False" with a Checked Box or an Unchecked box? Also I would like it to grey out the row when "True". Thanks in advance!

Here's some of my code that I tried:

Index.html.erb:
`

<h1 id="title">Project List</h1>



  <table>
    <thead>
      <tr id="headers">
        <th>Title</th>
        <th>Client</th>
        <th>Description</th>
        <th>Hours</th>
        <th>Done</th>
        <th colspan="3"></th>
      </tr>
    </thead>


    <tbody class="col-md-2" id="listItems">
      <% @projects.each do |project| %>
    <tr id="table">
      <td><%= project.title %></td>
      <td><%= project.client %></td>
      <td ><%= project.description %></td>
      <td><%= project.hours %></td>
      <td><%= check_box_tag "project_#{project.id}", "#{project.done}", "#{project.done? ? 'true':'false'", "#{project.done? ? 'true':'false'" %>
      </td>



          <td>
            <span title="Show">
              <%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %>
            </span>
          </td>


          <td>
            <span title="Edit">
              <%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %>
            </span>
          </td>

          <td>
            <span title="Delete">
              <%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %>
            </span>
          </td>

        </tr>
      <% end %>
    </tbody>
  </table>

  <br>

  <%= link_to 'New Project', new_project_path, id:"new" %>

`

1

1 Answers

2
votes

I'm surprised that the line <% if project.done("true") %> doesn't throw an error.

You'll need to show a check box so use the check_box_tag and from that you can display it being checked or not by testing if the project is done.

<%= check_box_tag "project_#{project.id}", "#{project.done}", project.done?, disabled: "#{project.done?}" %>

Taking these in order, this code will render a checkbox with:

  1. an id and name of project_id_of_project; and
  2. a value of 0 or 1 depending on whether the project is done or not; and
  3. it being checked if the project is done and unchecked if not; and
  4. the box disabled if project is done.

The marginally interesting code in this answer is:

"#{project.done? ? 'true':'false'"

This is an inline if/else. The part project.done? will return true or false and then output the part before the colon if true and the part after if false.