Forum: Ruby on Rails problem with a selfmade helper method

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.
Timo (Guest)
on 2006-01-23 16:59
(Received via mailing list)
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
Guest (Guest)
on 2006-01-23 17:15
In your ProgramOption's constructor, I think you have to use id and name
instead of @id and @name
Timo (Guest)
on 2006-01-23 17:41
(Received via mailing list)
Thanks for your quick answer.
Nope, that didn't do the trick. But I found, that

  if pt.id == pr.programtype

is never getting true. Strange...
Alex Y. (Guest)
on 2006-01-23 17:50
(Received via mailing list)
Timo wrote:
> Thanks for your quick answer.
> Nope, that didn't do the trick. But I found, that
>
>   if pt.id <http://pt.id/> == pr.programtype
>
> is never getting true. Strange...
That's because you're comparing a Fixnum to a ProgramType.  Try:
   if pt == pr.programtype
This topic is locked and can not be replied to.