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?
但是两者总是返回default_filename,即使在子类控制器中定义了filename_method也是如此。有人能指出我在正确的方向吗?
Thanks,
-C
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.
我猜这是因为控制器超类不知道子类的方法是否存在。您可以通过检查子类的methods数组来确认这一点。
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.
似乎解决方案是重新考虑您的设计。不要检查filename_method的存在,只需在基本文件名方法中提供默认文件名行为,并覆盖子类中的filename方法以提供自定义文件名功能。
For example, in your superclass:
例如,在您的超类中:
def filename
# return default file name
end
And in your subclass:
在你的子类中:
def filename
# return custom file name
end
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/05/06/82f59e1f0286001f7cddbe39d29f1c7a.html。