Brian Ablaza wrote the following on 03.05.2007 00:37 :
I swear this worked yesterday, but doesn't work today.
I have a method in application.rb:
def img_types ["jpeg", "tiff", "gif", "eps"] end
This is called from various controllers, e.g.,
if f.type in img_types ...
Shouldn't that work?
Well, today it doesn't. I changed it to
img_types = ["jpeg", "tiff", "gif", "eps"]
No go. The only thing that works is
$IMG_TYPES =["jpeg", "tiff", "gif", "eps"]
Then I can say
if f.type in $IMG_TYPES
...but I didn't have to do that yesterday.
Shouldn't I be able to call the global method to return the array?
Am I crazy? These image types should probably linked with a class. You probably have an Image class or something like it with an attribute which should be included in these values. The proper way to code this should be to have :
class Image VALID_TYPES = [ "jpeg", "tiff", "gif", "eps"] end
Then when you need to check a value, you'll have to do:
Image::VALID_TYPES.include? value
If for example your Image class is derived from ActiveRecord::Base with a image_type, you'll probably want something like:
class Image < ActiveRecord::Base VALID_TYPES = [ "jpeg", "tiff", "gif", "eps"]
validates_inclusion_of :image_type, VALID_TYPES end
VALID_TYPES is a constant defined in the Image class, in the Image context you can use "VALID_TYPES", in another context you need the "Image::VALID_TYPES" syntax to enforce the proper context.
Lionel.