Odd button behaviour

Working on an odd one here. I've got a simple scaffold that for some reason doesn't catch the correct data back on boolean fields. That's to say that every time I load the page, every single true/false menu option is set to false.

The scaffold generated the following for the boolean fields in the _form.rhtml file: <p><label for="consultant_active">Active</label><br/> <select id="consultant_active" name="consultant[active]"><option value="false">False</option><option value="true">True</option></

</p>

It's completely inert html with no possibility of pulling in the current field value so it makes sense that it's not truly interactive.

If I select True, it does get correctly passed back and written to the database, but if I don't touch it, the false value gets written to the database. So I thought that it would be more intelligent if these options were presented as radio buttons and see if I couldn't replace the html with some code.

I've tried a number of variations, and according to the documentation at http://edgedocs.planetargon.org/ I should do this: radio_button(object_name, method, tag_value, options = {}) Returns a radio button tag for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). If the current value of method is tag_value the radio button will be checked. Additional options on the input tag can be passed as a hash with options. Example (call, result). Imagine that @post.category returns "rails":

   radio_button("post", "category", "rails")    radio_button("post", "category", "java")      <input type="radio" id="post_category" name="post[category]" value="rails" checked="checked" />      <input type="radio" id="post_category" name="post[category]" value="java" />

However, when I supply

<%= radio_button("consultant","active","True") %> <%= radio_button("consultant","active","False") %>

I get: <input id="consultant_active_true" name="consultant[active]" type="radio" value="True" /> <input id="consultant_active_false" name="consultant[active]" type="radio" value="False" />

which doesn't match the documentation. And more importantly, it doesn't work :slight_smile:

The environment is: - ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1] - Rails 1.2.3

Can anyone shed some light on this behaviour?

Erik

Erik Ableson wrote:

<%= radio_button("consultant","active","True") %> <%= radio_button("consultant","active","False") %>

Instead write

<%= radio_button :consultant, :active, true %> <%= radio_button :consultant, :active, false %>

This works - many thanks for the tip. Any pointers to documentation that explains the why?

Cheers,

Erik

Erik Ableson wrote:

This works - many thanks for the tip. Any pointers to documentation that explains the why?

<%= radio_button :consultant, :active, true %> <%= radio_button :consultant, :active, false %>

The radio_button docs state that "If the current value of method is tag_value the radio button will be checked".

Your method returns a boolean, so the tag_values must be boolean to pass an equality test. The helper automatically turns the boolean values into strings for the HTML.