简体   繁体   中英

Rails form helpers

I am using a rails form helper for a dropdown list as follows :

<%= select_tag :analysis, options_for_select(%w[PENDING PASS FAIL], row3[7]), :disabled=> "true", 
:id => 'build_status#{index}', :style=> 'width:100px', :onchange => 'this.form.submit()'%> 

Now instead of having

:disabled=> "true",

I want to use a ruby variable such that

$tmp = "disabled"

And use this variable to make the dropdown list disabled. Because sometimes I want to keep it enabled and sometimes it will be disabled based on the value of the $tmp variable.

I am having trouble with the syntax. Can anyone test it out and see which syntax works properly?

When using the :disabled option, you need to pass it a boolean value, not a string. Given any string literal evaluates to true, both disabled: "true" and disabled: "false" will disable the tag. To not disable the element, you'd need to use disabled: false (not the lack of quotation marks). Perhaps this was the source of your problems?

To disable the element based on another variable, you can just put any operation that will give you a boolean. For example, to only disable the menu if $tmp is "disabled" , you could use something like disabled: ($tmp == "disabled") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM