如何禁用select标记中的选项并将选项设置为默认值

时间:2015-03-16 07:32:09

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我有一个select_tag并将一个选项值设为默认值,我想根据条件将某个选项设为禁用

<% disabled_option = ["options for disabled"]%> 
<%= select_tag "product_dropdown", options_for_select(ProductType.get_all_products, @type), {:disabled => disabled_options} %>

上面的代码抛出错误错误的参数数量(3为2) 甚至我用过

<%= select_tag "product_dropdown", options_for_select(ProductType.get_all_products, @type, {:disabled => disabled_options}) %>

这也不起作用我该怎么做

1 个答案:

答案 0 :(得分:0)

这是options_for_select

的语法
 options_for_select(["value1", "value2", "value3", "value4"], disabled: ["value1", "value4"])

你传递了三个arguments

options_for_select(ProductType.get_all_products, @type, {:disabled => disabled_options})

所以它应该如下所示,ProductType.get_all_products应该是array

修改

options_for_select(ProductType.get_all_products, selected: @type, disabled: disabled_options)