Use collection_select

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!

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

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!

On 8/18/06, M. R. [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs