2009/4/7 Helen Smith <[email protected]>

>
> 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'<http://security/transfers/transfer_help.htm%27>
> )"><img
> 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'<http://security/transfers/transfer_help.htm%27>
> )"><img
> 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>
> --
>

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/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 Bush

http://blog.web17.com.au
http://github.com/danielbush/sifs/tree/master
http://github.com/danielbush

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to