我有选择:
= f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})
上面这段代码显然会返回错误,但是如何根据 by 禁用选项id
?
我有选择:
= f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})
上面这段代码显然会返回错误,但是如何根据 by 禁用选项id
?
尚未对此进行测试,但在您的控制器中您不能这样做
@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})
<%= f.select :status, STATUSES.map{|s| [s.titleize, s]}, { disabled: DISABLED_STATUSES.map{|s| [s.titleize, s]} %>
最近遇到了这个问题并使用了以下解决方案:
#view
= f.select(:category_id, @filtered_categories, :html_options => {:class => 'select_box'}
#controller
@filtered_categories = Category.all.select do |category|
[logic here]
end
对于select_tag
,我们可以这样做:
<%= select_tag "sample", options_for_select(
[['A', 'a'], ['B', 'b'], ['C', 'c']]),
{class: 'form-control', disabled: data.present? == false ? true : false}
%>