Search code examples
rubyruby-on-rails-4

Displaying Checkboxes in Show View instead of True or False Values


I am creating an application for a security company I work for.. I have a few forms that are boolean value heavy and have a large number of checkboxes a user would go through to select various incident types / descriptions ect..

currently the checkbox i apply in my _form page is as follows:

<td><center><div class="control-group"><div class="controls"><%= f.check_box :on_site_owner %></div></div></center></td> 

The display on my Show Page is as follows:

<td><center><div class="control-group"><div class="controls"><%= @resident_owner.on_site_owner %></div></div></center></td>

What i am hoping to accomplish is one of two things. and either will work great i just have no idea how to achieve it... and cant find much direction on it.

1) Populate a list on the show page with only the selected or "true" boxes.

2) only show true values instead of true and false values on the show page..

i would really prefer option one for ease of reading and so the client that will view it doesn't go into a true / false overload.

Any help at all is greatly appreciated. thanks in advance.


Solution

  • For using checkboxes in show page, you can use check_box_tag helper method. Documentation here. Eg:

    <td>
     <center>
       <div class="control-group">
         <div class="controls">
           <%= check_box_tag "On Site Owner, @resident_owner.on_site_owner,  @resident_owner.on_site_owner,disabled: true %>
        </div>
      </div>
     </center>
    </td>
    

    But for better UI design, I would create a helper that generates check(the tick icon) or uncheck icon for given value and use it in erb. Eg:

    module ApplicationHelper
    
      def show_check_icon(boolean_value)
        if boolean_value then 
          "<i class='fa-li fa fa-check-square'></i>"
        else 
          "<i class='fa-li fa fa-square'></i>"
        end 
      end
    
    end
    

    Hope that helps.