3

我有选择:

= f.select(:category_id, @categories, :html_options => {:class => 'select_box'}, {:disabled => true if category.id == 18})

上面这段代码显然会返回错误,但是如何根据 by 禁用选项id

4

4 回答 4

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})
于 2012-07-06T14:55:38.847 回答
1
<%= f.select :status, STATUSES.map{|s| [s.titleize, s]}, { disabled: DISABLED_STATUSES.map{|s| [s.titleize, s]} %>
于 2013-07-03T08:01:30.203 回答
0

最近遇到了这个问题并使用了以下解决方案:

#view
= f.select(:category_id, @filtered_categories, :html_options => {:class => 'select_box'}

#controller
@filtered_categories = Category.all.select do |category|
  [logic here]
end
于 2014-01-06T11:49:01.017 回答
0

对于select_tag,我们可以这样做:

<%= select_tag "sample", options_for_select(
    [['A', 'a'], ['B', 'b'], ['C', 'c']]),
    {class: 'form-control', disabled: data.present? == false ? true : false}
%>
于 2018-09-12T02:21:55.673 回答