# Creating an array of key-value pairs for options\_for\_select

**URL:** <https://www.ruby-forum.com/t/creating-an-array-of-key-value-pairs-for-options-for-select/119170>\
**Category:** Rails\
**Created:** [November 4, 2007, 8:57am UTC](https://www.ruby-forum.com/t/creating-an-array-of-key-value-pairs-for-options-for-select/119170 "2007-11-04T08:57:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [November 4, 2007, 8:57am UTC](https://www.ruby-forum.com/t/creating-an-array-of-key-value-pairs-for-options-for-select/119170/1 "2007-11-04T08:57:50Z")

</div>

My situation is:

- I’m using a select\_tag form helper which shows a menu of names from  
a collection in a view
- options\_for\_select works nicely here because I can just pass the  
collection in

so, it looks like this

\<%= select\_tag( :group\_selection, options\_for\_select(@group\_names))  
%\>

problem:

some of the strings in @group\_names are too wide for the panel I want  
to display the menu in

- so, I thought I’d truncate the names for display, but I still need  
the full names so that the selection passed back to the controller in  
params[:group\_selection] will match the actual name of the group

what’s cool is that options\_for\_select will take an array of key-value  
pairs, so I can put the truncated name in the first as the key and the  
original as the value and it works… when I hard code up an array as  
a test

I want to write a helper function that would take @group\_names and a  
truncation length and would return the array of key-value pairs  
described above… and that would be passed into options\_for\_select.

This might be a simple ruby problem, but I havent found how to write  
the code to do.

Any help will be much appreciated.
