# Odd button behaviour

**URL:** <https://discuss.rubyonrails.org/t/odd-button-behaviour/11851>\
**Category:** rubyonrails-talk\
**Created:** [April 18, 2007, 7:33pm UTC](https://discuss.rubyonrails.org/t/odd-button-behaviour/11851 "2007-04-18T19:33:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Ableson](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@Erik\_Ableson](https://discuss.rubyonrails.org/u/Erik_Ableson)\
**Post date:** [April 18, 2007, 7:33pm UTC](https://discuss.rubyonrails.org/t/odd-button-behaviour/11851/1 "2007-04-18T19:33:50Z")

</div>

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/](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":

&nbsp;&nbsp;&nbsp;radio\_button("post", "category", "rails") &nbsp;&nbsp;&nbsp;radio\_button("post", "category", "java") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<input type="radio" id="post\_category" name="post[category]" value="rails" checked="checked" /\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<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 🙂

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

---

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [April 19, 2007, 3:02am UTC](https://discuss.rubyonrails.org/t/odd-button-behaviour/11851/2 "2007-04-19T03:02:19Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![Erik\_Ableson](https://avatars.discourse-cdn.com/v4/letter/e/9fc348/32.png) [@Erik\_Ableson](https://discuss.rubyonrails.org/u/Erik_Ableson)\
**Post date:** [April 19, 2007, 6:26am UTC](https://discuss.rubyonrails.org/t/odd-button-behaviour/11851/3 "2007-04-19T06:26:50Z")

</div>

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

Cheers,

Erik

---

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [April 19, 2007, 8:10am UTC](https://discuss.rubyonrails.org/t/odd-button-behaviour/11851/4 "2007-04-19T08:10:27Z")

</div>

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.
