# Select for boolean attribute

**URL:** <https://www.ruby-forum.com/t/select-for-boolean-attribute/171062>\
**Category:** Rails\
**Created:** [June 25, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/select-for-boolean-attribute/171062 "2009-06-25T22:45:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 25, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/select-for-boolean-attribute/171062/1 "2009-06-25T22:45:40Z")

</div>

I would like to use a select box to display and edit a boolean model  
attribute. I realize that perhaps checkboxes or radio buttons are a  
more idomatic way to do this in html but I would like to use a select  
box.

What I want to do is to map the selected value in the box to the  
existing value of the attribute when the form displays. I am having  
trouble figuring out how do to this and the examples I can find are  
not explicit enough for me to discern how this is done, or if it is  
possible.

I have discovered that if one prefaces the select with the form  
partial variable ‘f’ then one gets a series of obsure errors about an  
undefined ‘merge’ method, while removing this and going with a bare  
select call avoids the problem and otherwise seems to work. Odd, but  
there it is.

Consequently, presently the structure looks something like this:

```
        <%= select :is_invoicable,
          [
            ['Yes', 'true'],
            ['No', 'false'],
          ],
          {}, # select options
          :id => 'select_is_invoicable',
          :size => 1,
          :title => 'May invoices be issued in this currency?'
        -%>
</p>

```

What I want to accomplish, of course, is to have the current value of  
is\_invoicable (true or false) used to set the selected attribute of  
the option to either Yes or No as appropriate. I nonetheless want  
both options available in the drop down. I cannot seem to hit upon  
the exact syntax to do this and the examples in the options\_for\_select  
api do not provide me with much guidance. From them I gathered that  
this would work:

```
          ],
          currency_detail.is_invoicabe.to_s, # select options
          :id => 'select_is_invoicable',

```

But that produces this in the view:

```
        <select id="is_invoicable_YestrueNofalse"

```

name=“is\_invoicable[YestrueNofalse]”\>  
false

When what I was looking for was this:

```
        <select id="select_is_invoicable" name="currency

```

[is\_invoicable]" size=“1”  
title=“May invoices be issued in this  
currency?”\>  
Yes  
No\</  
select\>

I would really appreciate guidance on how this is done.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 25, 2009, 11:50pm UTC](https://www.ruby-forum.com/t/select-for-boolean-attribute/171062/2 "2009-06-25T23:50:43Z")

</div>

If you’re doing form\_for something like this will work.

Of course, I’m using a

\<% form\_for(@your\_model\_here) do |f| %\>  
\<%= f.error\_messages %\>

\<%= f.label :is\_invoicable%\>  
 \<%= f.select :is\_invoicable, {"Yes" =\> true, "No" =\> false}%\>

\<%= f.submit 'Update' %\>

\<% end %\>

Of course, you’ll need to add whatever html options you need, but  
whatever the object’s attributes are will be the selected value.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [June 26, 2009, 12:31am UTC](https://www.ruby-forum.com/t/select-for-boolean-attribute/171062/3 "2009-06-26T00:31:44Z")

</div>

Yuck, I apologize for the horrible sentences in my reply… it is the  
end of the day.
