Forum: Ruby on Rails Trying to make an app helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris H. (Guest)
on 2005-12-19 08:12
Any ideas with fixing this? Doesn't work properly, although it doesn't
throw any runtime errors.


# Methods added to this helper will be available to all templates in the
application.
module ApplicationHelper
	def options_for_months(selected_month)
		months=["January","February","March","April","May","June","July","August","September","October","November","December"]
		curmonth=1
		o=""
		months.each do |month|
			if curmonth == selected_month
				o << "<option value='" << curmonth << "' selected='selected'>" <<
month << "</option>"
			else
				o << "<option value='" << curmonth << "' selected='selected'>" <<
month << "</option>"
			end
			curmonth = curmonth + 1
		end
		o
	end
end
Chris H. (Guest)
on 2005-12-19 08:14
Other than the obvious 'selected=selected' on both the true and false
sides of the if statement, of course...

I seem to find that the option value='X' - the X is gibberish, it should
be 1..12
Ed C. (Guest)
on 2005-12-19 08:18
(Received via mailing list)
Hi Chris --

It looks like you have the HTML "selected" attribute set in both the
IF and ELSE (which would cause the first month in the list to always
be selected in the dropdown box.)

Hope that helps,
EJC
Chris H. (Guest)
on 2005-12-20 00:02
Solved it. I just needed to add the to_s to the curmonth, to convert it
from an int to a string (i would have thought that was automatic?) :
<< curmonth.to_s <<

And of course the 'selected' on both sides of the if statement, but it
took me 5 seconds to realise that...
This topic is locked and can not be replied to.