Trying to disable the whole f.select in rails

I'm trying to disable a dropdown field in my form, but with the option disable: true the field is still clickable, thanks for the help!

f.select:

<%= f.select(:point_constraint_id, @point_constraints.collect {|u| [u.name, u.id]}, :prompt => 'Select', label: t('point.operational_limitions') + ' *', label_class: "light-text", disabled: true, required: true )%>

1 Answer

Ruby reads hash like parameters in the parameter list as one hash when they are at the end of the parameter list. Therefore all options are passed to the options hash. But the signature of the select method looks like this:

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

Therefore you need to separate the options from the html_options to help Ruby to understand that disable: true is actually a html_option:

<%= f.select( :point_constraint_id, @point_constraints.collect { |u| [u.name, u.id] }, { prompt: 'Select', label: "#{t('point.operational_limitions')}*", label_class: "light-text", required: true }, { disabled: true } ) %>

Note the last {...} around the disabled: true is actually not needed. I added it to make it clearer that you need to pass two hashes to that method. One with the options for the select, another for pure the HTML options.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like