# Displaying 'ticked' check boxes

**URL:** <https://www.ruby-forum.com/t/displaying-ticked-check-boxes/164908>\
**Category:** Rails\
**Created:** [April 7, 2009, 9:53am UTC](https://www.ruby-forum.com/t/displaying-ticked-check-boxes/164908 "2009-04-07T09:53:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![helensmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@helensmith](https://www.ruby-forum.com/u/helensmith)\
**Post date:** [April 7, 2009, 9:53am UTC](https://www.ruby-forum.com/t/displaying-ticked-check-boxes/164908/1 "2009-04-07T09:53:32Z")

</div>

I need to display check boxes as ‘ticked’ in my edit function where  
there is a corresponding value for them in the database.

In my new function a user can select between 1 and 4 tick boxes and I  
need these ticked check boxes displayed back to them when they go in to  
the edit function for that database item.

Most appreciated if anyone out there can help please?

Here’s the code from the new function that sets up the database fields  
from the check boxes - so I just need to display these as ‘ticked’ in my  
edit function.

tr\>

Agreement Type &nbsp Contract &nbsp &nbsp Data Transfer Agreement &nbsp &nbsp Data Destruction Agreement&nbsp &nbsp Non-Disclosure Agreement&nbsp &nbsp

Currently my edit function is as below but I’m missing the bit to get  
the boxes ticked.

Agreement Type &nbsp Contract &nbsp &nbsp Data Transfer Agreement &nbsp &nbsp ""\>Data Destruction Agreement&nbsp &nbsp ""\>Non-Disclosure Agreement&nbsp &nbsp

---

<div class="post-metadata">

**Author:** ![helensmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a87d85/32.png) [@helensmith](https://www.ruby-forum.com/u/helensmith)\
**Post date:** [April 8, 2009, 6:43am UTC](https://www.ruby-forum.com/t/displaying-ticked-check-boxes/164908/2 "2009-04-08T06:43:52Z")

</div>

2009/4/7 Helen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Here’s the code from the new function that sets up the database fields
> 
> --

Hi Helen,  
Had a bit of trouble reading the last section.  
To check a checkbox, you have to say:  
\<input type=“checkbox” … checked=“checked” /\>  
The “value” attribute can be whatever you set it, but maybe use the  
rails  
default which is 0 and 1 for not-ticked and ticked respectively. Bear  
in  
mind that the html spec says that if you don’t tick the box, it won’t  
get  
sent back to the server so other things being equal, your server won’t  
get  
to see 0.

You should read  
[ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html) as  
a  
starting point for what rails can do to help you build forms and in  
particular check out the ‘check\_box’ function which employs a little  
hack to  
get round the html spec so that you do see ‘0’.

Ideally, you want something a little like this:  
\<% form\_for :transfer do |f| %\>  
…  
\<%= f.checkbox ‘agreement\_1’ %\>  
…  
\<% end %\>  
I’m assuming you have a @transfer model or object which is created in  
your  
controller action with an ‘agreement\_1’ method that returns a boolean or  
something like that.

You can of course create the checkbox manually, but you’ll need to write  
code to test the boolean and insert the checked attribute/value pair  
into  
your tag:  
\<%  
checked=""i  
checked = %{checked=“checked”} if agreement\_1  
%\>  
\<input … \<%=checked%\> /\>

–  
Daniel B.

[http://blog.web17.com.au](http://blog.web17.com.au)

> **[GitHub - danielbush/sifs: The Simple Include-File System (for bash)](https://github.com/danielbush/sifs/tree/master)**
>
> master

> **[danielbush - Overview](https://github.com/danielbush)**
>
> danielbush has 33 repositories available. Follow their code on GitHub.
