# Use collection\_select

**URL:** <https://www.ruby-forum.com/t/use-collection-select/65313>\
**Category:** Rails\
**Created:** [August 17, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/use-collection-select/65313 "2006-08-17T14:44:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 17, 2006, 2:44pm UTC](https://www.ruby-forum.com/t/use-collection-select/65313/1 "2006-08-17T14:44:46Z")

</div>

I use the following code in my view:

\<%= collection\_select(‘datedata’, ‘date’, @allDate, :id, :date) %\>

It’s show me the date of all entries in the database (Dropdown menu).  
Additional, I wrote a helper method “showDate” in the  
application\_helper.rb! How can I use this method in the  
collection\_select for every date entry?

thanks for your anwser!

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 17, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/use-collection-select/65313/2 "2006-08-17T23:36:29Z")

</div>

What is it that you’re trying to accomplish with your ‘showDate’ method?

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 18, 2006, 10:43am UTC](https://www.ruby-forum.com/t/use-collection-select/65313/3 "2006-08-18T10:43:49Z")

</div>

FranÃ§ois Montel wrote:

> What is it that you’re trying to accomplish with your ‘showDate’ method?

I would like to show the date in my favorite style. The following code  
is the used method:

def showDate(d)  
d.strftime("%d.%m.%y")  
end

I want to display the date in the collection with the method!

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [August 18, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/use-collection-select/65313/4 "2006-08-18T15:32:28Z")

</div>

On 8/18/06, M. R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to display the date in the collection with the method!

The first idea that comes to mind is to modify your collection\_select  
call:  
\<%= collection\_select(‘datedata’, ‘date’, @allDate.collect{|d|  
showDate(d)},  
:id, :date) %\>

But that’s a little ugly. You could modify your helper thusly:  
def showDate(d=Time.now)  
date\_format = “%d.%m.%y”  
if Array === d  
d.collect {|date| date.strftime(date\_format)}  
else  
d.strftime(date\_format)  
end  
end

This lets you pass the helper a Date object or an array of Dates, and  
get  
back what you’d expect.
