Hi all,
i tried to set up the helper method “grouped_programs_selector” in
application_helpers.rb. Since i dont have a deep understanding of Ruby
itself, I pretty much copied the Grouped Selection List (Chapter 17, pp.
259, 260) example from Dave & David’s Book.
The optiongroups are properly created by my method, but there are no
options
inside. Maybe you can debug this one?
-snip- from index.rhtml
<%=
option_groups_from_collection_for_select(
grouped_programs_selector(:programs, :programtypes), # calling
application_helper - method.
:options, :type_name,
:id, :name,
@programs)
%>
-snap- application_helper.rb
module ApplicationHelper
class ProgramOption
attr_reader :attributes
@attributes = []
def initialize(id, name)
@attributes = Struct.new(@id, @name)
end
end
class ProgramType
attr_reader :type_name, :options
def initialize(name)
@type_name=name
@options=[]
end
def <<(option)
@options << option
end
end
The actual helper method
def grouped_programs_selector(programs, programtypes)
groups = []
@programtypes.each do |pt|
newgroup = ProgramType.new(pt.name)
@programs.each do |pr|
if pt.id == pr.programtype
newgroup << ProgramOption.new(pr.id, pr.name)
end
end
groups << newgroup
end
return groups
end
end