How to disable a select?

I want to use form template to render data for show.

My idea is, to readonly/disable formelements. This works fine, except
with selects. The following three variants of render code do not emit
the keyword disabled to the html text:

f.select :attr, @choices, :include_blank => true,
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
{:disabled => !@condition}

Writing readonly instead if disabled (which seems to be undefined for
select) also doesn’t have any effects.

What is going wrong?

Fritz T. wrote:

I want to use form template to render data for show.

Generally not a good idea from a UI point of view. Just use plain text
for display, unless you want to allow editing.

My idea is, to readonly/disable formelements. This works fine, except
with selects. The following three variants of render code do not emit
the keyword disabled to the html text:

f.select :attr, @choices, :include_blank => true,
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
{:disabled => !@condition}

Writing readonly instead if disabled (which seems to be undefined for
select) also doesn’t have any effects.

What is going wrong?

Are you sure !@condition is true when you expect it to be?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Are you sure !@condition is true when you expect it to be?

Yes, I checked it in the debugger.

Try

f.select :attr, @choices, {:include_blank => true}, { :disabled =>
!@condition }

f.select :attr, @choices, :include_blank => true,
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
:disabled => !@condition
f.select :attr, @choices, {:include_blank => true},
{:disabled => !@condition}

I remember struggling with this a while back too. This worked for me:

<%= f.select :attr,
@choices,
{},
:include_blank => true,
:disabled => !@condition %>

I believe the problem lies with the empty hash for the options. This
is the signature from the documentation:

select(object, method, choices, options = {}, html_options = {})

<%= f.select :attr,
@choices,
{},
:include_blank => true,
:disabled => !@condition %>

It just happens that I’m working on the very same thing again and the
problem with the above is that when the select is not disabled it
actually does not include the empty option.

Running Rails 2.3.5 & Ruby 1.8.6