【发布时间】:2017-05-26 05:50:21
【问题描述】:
我有一个选择标签,它也使用 Select2 以下拉方式显示一些类别
<%= category.select :parent_id, nested_set_options(Category, @category) {|i| if !(i.depth >= 2)
"#{i.root.name if !i.parent_id.nil? } #{'>' unless !i.parent_id} #{i.name}"
end
} , {include_blank:true} , class: 'select2 form-control'%>
当您决定将其嵌套在父类别下时,此选择是您创建新类别时的提示。
我使用此代码的原因是因为我只想让管理员选择嵌套直到depth=3 所以只有Parent -> Kid -> GrandKid
代码运行良好,但是当决定不显示某个值时,我得到一个空白(要小得多),但用户仍然可以选择它。
有什么办法可以排除那些不属于这种情况的值吗?
解决这个问题的代码是对 GoGoCarl 的最佳答案的解释,只是因为我对 reject_if 有一个小问题
所以我只是扭转了整个局面,结果是这样的:
<%= category.select :parent_id, nested_set_options(Category, @category) {|i|
if !(i.depth >= 2)
"#{i.root.name if !i.parent_id.nil? } #{'>' unless !i.parent_id} #{i.name}"
end
}.select { |i|
!i[0].nil? || !i[0].blank?
} , {include_blank:true , include_hidden:false} , class: 'select2 form-control'%>
【问题讨论】:
标签: ruby-on-rails ruby rubygems