Ruby on rails Rails-在散列中使用条件

Ruby on rails Rails-在散列中使用条件,ruby-on-rails,Ruby On Rails,我试图在内容\u标记散列中使用一个条件语句,这样当'hdrs'包含'fields'中的任何值时,:class=>'foo'将设置为:class=>'bar'。是否可以在content\u标记hash中放入内联条件?如果是,怎么做 fields = ['name'] headers.collect { |hdrs| content_tag('th', hdrs, {:nowrap=>true, :class=>'foo', :name=>hdrs}) } 为什么不调用另一个包

我试图在
内容\u标记
散列中使用一个条件语句,这样当
'hdrs'
包含
'fields'
中的任何值时,
:class=>'foo'
将设置为
:class=>'bar'
。是否可以在
content\u标记
hash中放入内联条件?如果是,怎么做

fields = ['name']

headers.collect { |hdrs| content_tag('th', hdrs, {:nowrap=>true, :class=>'foo', :name=>hdrs}) }

为什么不调用另一个包含逻辑的方法呢?
headers.collect { |hdrs| content_tag('th', hdrs, {:nowrap=>true, :class=>(fields.include?(hdrs) ? 'bar' : 'foo'), :name=>hdrs}) }