2

I want to disable an HTML DropDown in Rails, and I found this solution: how to disable the entire dropdown control in html

So, I have this:

f.select( ...., :disabled => true)

But, the problem is, when the DropDown is disabled, it does not show in the params collection.

EDIT:
This is my situation:
I have a form with a text_field and a select field. There are two cases:

  • The user creates a new item directly. If so, she will choose a category from the select field.
  • The user creates a new item, after redirecting from a category page. In this case, the select field is set to the value of the category, and should be disabled.

Does anyone have any idea how to fix this?

Community
  • 1
  • 1
Omid Kamangar
  • 5,768
  • 9
  • 40
  • 69
  • What are you expecting to show in the params collection? Do you want a default value? – Kyle C Jul 31 '12 at 15:53
  • Disabled form elements won't submit their value with a form submission. Take a look at this question and answers: http://stackoverflow.com/questions/1191113/disable-select-form-field-but-still-send-the-value – BaronVonBraun Jul 31 '12 at 16:32

2 Answers2

3

As mentioned in my comment in the original question, disabled form element values won't be present in the params on a form submission. To get around this, try using a hidden field to hold and submit the value you want.

Assuming some variables and whatnot are setup to help decide whether the select field should be disabled:

# ...
f.select(..., :disabled => @category_already_chosen)
f.hidden_field(...) if @category_already_chosen
# ...

Obviously this can be changed to suit your needs, but the basic idea is there. If you've already chosen a category and want the select field to be disabled, make a hidden field. If you haven't chosen a category, omit the hidden field and allow users to make use of the select field.

As shown in the link I posted, this is probably the simplest way to get around this limitation, without resorting to using Javascript to play with parameters after form submission.

Community
  • 1
  • 1
BaronVonBraun
  • 4,285
  • 23
  • 23
0

I would add query string parameters to your redirect_to call

redirect_to :action => form_path, :category => :whatever_category_they_came_from, :disabled => true

Then in your form

:selected => params[:category], :disabled => params[:disabled]
Kyle C
  • 4,077
  • 2
  • 31
  • 34