views:

64

answers:

3

View

<%= collection_select(@table, "sp", @pops, "col2", "col2", {:prompt => "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

A: 

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

Tass
Where I can apply "Hash[*col2.zip(col3)]"? View or Controller? Kindly mention it in above query,,,
Palani Kannan
I've got no clue about `collection_select`, but with [formtastic](http://github.com/justinfrench/formtastic), see Usage lowest quite at the end of the section.
Tass
+2  A: 

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?

Hoa
@Hoa: Exactly... But it prints only col2a, col2b.
Palani Kannan
When I replace col2 and combined value positions in collection_select,,, it works great.... God thanks....
Palani Kannan
Palani Kannan
In your controller, you can do this to unescape those characters: CGI::unescapeHTML(params[:id]).
Hoa
@Hoa: No... I need to apply without help of any modules. its possible in html? any idea?
Palani Kannan
A: 

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...

Palani Kannan
If my suggestion works for you, please vote for it. Thanks.
Hoa
@Hoa: Sure... But my reputation score restrict me to not vote any answers...
Palani Kannan
You should accept the correct answer, not repeat the correct answer and accept your version of it!
Mr. Matt
@Mr. matt Done.
Palani Kannan