Forum: Ruby on Rails Displaying 'ticked' check boxes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Helen S. (Guest)
on 2009-04-07 11:53
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>
<td nowrap><font size="2" color="#9E0B0E">Agreement Type &nbsp <a
href="javascript:openNewWindow('http://security/transfers/transfer_help.htm')"><im...
src="http://security/transfers/images/info.jpg" width="10" height="10"
border="0"></a></td>
<td colspan="2" nowrap><font size="2">
<input type="checkbox" name="transfer[agreement_1]" value="Y">Contract
&nbsp &nbsp
<input type="checkbox" name="transfer[agreement_2]" value="Y">Data
Transfer Agreement &nbsp &nbsp
<input type="checkbox" name="transfer[agreement_3]" value="Y">Data
Destruction Agreement&nbsp &nbsp
<input type="checkbox" name="transfer[agreement_4]"
value="Y">Non-Disclosure Agreement&nbsp &nbsp
</td>
</tr>

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

<tr>
<td nowrap><font size="2" color="#9E0B0E">Agreement Type &nbsp <a
href="javascript:openNewWindow('http://security/transfers/transfer_help.htm')"><im...
src="http://security/transfers/images/info.jpg" width="10" height="10"
border="0"></a></td>
<td colspan="2" nowrap><font size="2">
<input type="checkbox" name="transfer[agreement_1]" value=value="<%=
@transfers.agreement_1 if @transfers.agreement_1 %>">Contract &nbsp
&nbsp
<input type="checkbox" name="transfer[agreement_2]" value=value="<%=
@transfers.agreement_2 %>"">Data Transfer Agreement &nbsp &nbsp
<input type="checkbox" name="transfer[agreement_3]" value="value="<%=
@transfers.agreement_3 %>"">Data Destruction Agreement&nbsp &nbsp
<input type="checkbox" name="transfer[agreement_4]" value="value="<%=
@transfers.agreement_4%>"">Non-Disclosure Agreement&nbsp &nbsp
</td>
</tr>
Daniel B. (Guest)
on 2009-04-08 08:43
(Received via mailing list)
2009/4/7 Helen S. <removed_email_address@domain.invalid>

> Here's the code from the new function that sets up the database fields
> <td colspan="2" nowrap><font size="2">
>
> <td colspan="2" nowrap><font size="2">
> </tr>
> --
>

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
http://api.rubyonrails.org/classes/ActionView/Help... 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://github.com/danielbush/sifs/tree/master
http://github.com/danielbush
This topic is locked and can not be replied to.