开发者

Populate Two Columns in single collection_select Rails

View

<%= collection_select(@table, "sp", @pops, "col2", "col2", {:prompt =&开发者_StackOverflow中文版gt; "Select one"}) %>

Controller

@pops = Table.find(:all, :conditions=>{:col1 => "xyz"}, :order=> 'col2', :select=> 'DISTINCT col2')

This is my existing code. I am collecting a values in a column2 and populating it. Existing populating values as (col2a,col2b,col2c)

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


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

In the view, the collection_select is as follows

<%= collection_select(@table, "sp", @pops, "combined_value", "col2", {:prompt => "Select one"}) %>

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>

Is that what you want?


So you use col2 as keys and col3 as values? Hash[*col2.zip(col3)]


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

In the view, the collection_select is as follows

<%= collection_select(@table, "sp", @pops, "col2", "combined_value", {:prompt => "Select one"}) %>

The generated HTML will look like this

Select one col2a[col3a] col2b[col3b] col2c[col3c]

Is this I want... My questions may confused someone... Sorry for that... God thanks to Hoa... Its Working as my requirement...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜