I have a form_tag in my view and a few text_field_tag nested in it. I do not want the user to edit the text fields so I did :disabled => true.
The problem: If I do :disabled => true the text field value is not passed in params hash, while if I do :readonly => true it does send the values in params hash
Is this expected? If yes then there is no mention of this in the documentation. If not, then should I file a issue in GitHub?
Yes, that is the expected behavior. It is probably not mentioned in the Rails documentation because the disabled
and readonly
control behavior is defined by the W3C spec.
See the W3C documentation for Disabled controls, which states "disabled controls cannot be successful". A "successful" control is defined as being "'valid' for submission." Setting disabled
to true
on an input field in Rails will not cause it to not be submitted.
Read-only controls, however, can be successful. Therefore, if you wanted the value to be submitted, but not editable, you should use readonly
set to true
.
Yes, this is expected, but it has nothing to do with Rails. Per the HTML5 spec, form fields that are disabled
are skipped when the form is submitted (see 4.10.22.4 Constructing the form data set).
If you don't want the user to be able to edit the field, but still want its data to be submitted with the form, you should use the readonly
attribute, e.g.:
<%= text_field_tag 'year', '2015', readonly: true %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With