Forum: Ruby on Rails Problems with application level variables

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
79751cb27cd2de7d24fe8445422b7f4c?d=identicon&s=25 krunk- (Guest)
on 2006-12-31 00:38
(Received via mailing list)

I created a class called MenuLink so I could create link objects for my
navigation menus. I want to instantiate the list and have it available
to all controllers in my applcation.

How I did it:

I created a file called menu_link.rb in app/models that contained

class MenuLink
  include Enumerable
  attr_accessor :name, :method

  @@items = []

  def initialize()

  def self.each(&block)

Then in my app/controllers/application.rb I put

  before_filter :config_defaults

  def config_defaults
    # More on this function later
###################'HOME','welcome')'ABOUT US', 'about')'SCHEDULE', 'schedule')'YOGA INFO', 'info')'PHOTO GALLERY', 'gallery')'TEACHER TRAINING', 'training')'CONTACT US', 'contact')

Then in my templates I can do things like MenuLink.each {do stuff}

Now this seems to work alright......except for one thing.

Every time you refresh the page, any page, it creates a whole new set
of MenuLink objects and appends them to the total @@items list.

So I made a reset function for MenuLink

def self.reset
  @@items = []

So what I'm wondering is:

1) Is there a more correct way to handle this? Perhaps a filter that
only executes a single time and not with every refresh?

2) In ruby, is this the correct way to 'destroy' objects?
This topic is locked and can not be replied to.