在单个 collection_select Rails 中填充两列 [英] Populate Two Columns in single collection_select Rails
问题描述
查看
<%= collection_select(@table, "sp", @pops, "col2", "col2", {:prompt => "Select one"}) %>
控制器
@pops = Table.find(:all, :conditions=>{:col1 => "xyz"}, :order=> 'col2', :select=> 'DISTINCT col2')
这是我现有的代码.我正在收集 column2 中的值并填充它.现有填充值为 (col2a,col2b,col2c)
This is my existing code. I am collecting a values in a column2 and populating it. Existing populating values as (col2a,col2b,col2c)
现在,我希望在单个 collection_select 中填充两列 col2 和 col3.我希望像(col2a[col3a],col2b[col3b],col2c[col3c]).请给我一个想法,在单个集合中填充两列选择
Now, I wish to populate two columns col2 and col3 in a single collection_select. I wish to populate like (col2a[col3a],col2b[col3b],col2c[col3c]). Kindly give me the idea to populate two columns in single collection select
推荐答案
在 Table 模型中,添加以下方法(将其命名为适合您的应用程序,我将其命名为combined_value")
In the Table model, add the following method (name it whatever suits your app, I name it "combined_value")
def combined_value
"#{self.col2}[#{self.col3}]"
end
视图中collection_select如下
In the view, the collection_select is as follows
<%= collection_select(@table, "sp", @pops, "combined_value", "col2", {:prompt => "Select one"}) %>
生成的 HTML 如下所示
The generated HTML will look like this
<select name="table[sp]">
<option value="">Select one</option>
<option value="col2a[col3a]">col2a</option>
<option value="col2b[col3b]">col2b</option>
<option value="col2c[col3c]">col2c</option>
</select>
这是你想要的吗?
这篇关于在单个 collection_select Rails 中填充两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!