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:
</b><br />
<%= 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]">
<option
value="false">false</option></select>
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?">
<option value="true">Yes</option>
<option selected="selected"
value="false">No</option></
select>
I would really appreciate guidance on how this is done.
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 %>
<p>
<%= f.label :is_invoicable%><br />
<%= f.select :is_invoicable, {"Yes" => true, "No"
=> false}%>
</p>
<p>
<%= f.submit ''Update'' %>
</p>
<% 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.
On Jun 25, 10:08 am, byrnejb
<byrn...-fqAF1SpE7daWxEb2tNKyrg@public.gmane.org>
wrote:> 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:
>
> </b><br />
>
> <%= 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]">
> <option
value="false">false</option></select>
>
> 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?">
> <option value="true">Yes</option>
> <option selected="selected"
value="false">No</option></
> select>
>
> I would really appreciate guidance on how this is done.
Yuck, I apologize for the horrible sentences in my reply... it is the end of the day. On Jun 25, 4:50 pm, John Yerhot <joyer...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 %> > > <p> > <%= f.label :is_invoicable%><br /> > <%= f.select :is_invoicable, {"Yes" => true, "No" => false}%> > </p> > <p> > <%= f.submit ''Update'' %> > </p> > <% 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. > > On Jun 25, 10:08 am, byrnejb <byrn...-fqAF1SpE7daWxEb2tNKyrg@public.gmane.org> wrote: > > > 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: > > > </b><br /> > > > <%= 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]"> > > <option value="false">false</option></select> > > > 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?"> > > <option value="true">Yes</option> > > <option selected="selected" value="false">No</option></ > > select> > > > I would really appreciate guidance on how this is done.