我选择了:
= f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})
上面的代码显然会返回错误,但如何根据id
禁用选项?
答案 0 :(得分:4)
尚未对此进行测试,但在您的控制器中可以不进行测试
@checkvar = @category.id == 18 ? true : false
然后在视图中
f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => @checkvar})
或在模型中编写一个测试函数
def disable_select
if self.id == 18
true
else
false
end
end
然后在视图中
f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => @category.disable_select})
答案 1 :(得分:1)
<%= f.select :status, STATUSES.map{|s| [s.titleize, s]}, { disabled: DISABLED_STATUSES.map{|s| [s.titleize, s]} %>
答案 2 :(得分:0)
最近进入这个并使用以下解决方案:
#view
= f.select(:category_id, @filtered_categories, :html_options => {:class => 'select_box'}
#controller
@filtered_categories = Category.all.select do |category|
[logic here]
end
答案 3 :(得分:0)
对于select_tag
,我们可以这样做:
<%= select_tag "sample", options_for_select(
[['A', 'a'], ['B', 'b'], ['C', 'c']]),
{class: 'form-control', disabled: data.present? == false ? true : false}
%>