Reputation: 5370
In my application controller, I have a method that is supposed to check for the existence of another method in a subclassed controller to set the filename of a downloaded file, but i'm having trouble making it work properly
i've tried
def filename
begin
send "filename_method"
rescue NoMethodError
default_filename
end
end
and
def filename
if respond_to?("filename_method")
send "filename_method"
else
default_filename
end
end
but both always return default_filename, even if filename_method is defined in the subclassed controller. Can someone point me in the right direction here?
Thanks,
-C
Upvotes: 0
Views: 127
Reputation: 838
I'm guessing this is due to the fact that the controller superclass does not know of the existence of the subclass's methods. You can confirm this by inspecting the methods
array of the subclass.
It seems like the solution is to reconsider your design. Instead of checking for the existence of filename_method
, just provide the default filename behavior in the base filename
method and override the filename
method in the subclass to provide custom filename functionality.
For example, in your superclass:
def filename
# return default file name
end
And in your subclass:
def filename
# return custom file name
end
Upvotes: 2