# Implement collection\_select based upon an array

**URL:** <https://www.ruby-forum.com/t/implement-collection-select-based-upon-an-array/233288>\
**Category:** Rails\
**Created:** [December 1, 2014, 11:28am UTC](https://www.ruby-forum.com/t/implement-collection-select-based-upon-an-array/233288 "2014-12-01T11:28:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [December 1, 2014, 11:28am UTC](https://www.ruby-forum.com/t/implement-collection-select-based-upon-an-array/233288/1 "2014-12-01T11:28:50Z")

</div>

Hello,

I have created an array of time slots as strings and am trying to  
display them in a form to be selected upon creation.

This is my Appointments controller file.

helper\_method :timeslots

def timeslots

@timeslots = [‘8:30 AM’,‘9:00 AM’,‘9:30 AM’,‘10:00 AM’,‘10:30  
AM’,‘11:00 AM’,‘11:30 AM’, ‘12:00 PM’, ‘12:30 PM’, ‘1:00 PM’,‘1:30 PM’,  
‘2:00 PM’, ‘2:30 PM’, ‘3:00 PM’, ‘3:30 PM’,‘4:00 PM’]

end

This is my Appointments form.html.erb file

 \<%= f.label :appointment\_time %\>  
 \<%= select "obj", "test", timeslots.each\_with\_index.map { |name, index|} %\>

On the form’s page, I just see a blank box with no values. Every time I  
add a value, the box gets bigger, but there are no values displayed in  
the rows of the select box.

---

<div class="post-metadata">

**Author:** ![blackstones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@blackstones](https://www.ruby-forum.com/u/blackstones)\
**Post date:** [December 1, 2014, 12:26pm UTC](https://www.ruby-forum.com/t/implement-collection-select-based-upon-an-array/233288/2 "2014-12-01T12:26:03Z")

</div>

Each / Each with index returns the same array . You need to use map.

> <https://stackoverflow.com/questions/11596879/why-does-arrayeach-return-an-array-with-the-same-elements>

Can you try this

\<%= select “obj”, “test”, @timeslots.map.with\_index{ |name, index|  
[name,  
index] } %\>

On Mon, Dec 1, 2014 at 3:58 PM, Dmoneyzzz D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![blackstones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f04885/32.png) [@blackstones](https://www.ruby-forum.com/u/blackstones)\
**Post date:** [December 1, 2014, 2:17pm UTC](https://www.ruby-forum.com/t/implement-collection-select-based-upon-an-array/233288/3 "2014-12-01T14:17:24Z")

</div>

Vivek S. wrote in post #1163711:

> \<%= select “obj”, “test”, @timeslots.map.with\_index{ |name, index|  
> [name,  
> index] } %\>
> 
> On Mon, Dec 1, 2014 at 3:58 PM, Dmoneyzzz D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks, it worked
