如果rails中不存在方法,我正在尝试返回一些东西。
我拥有的红宝石模型如下所示:
class myModel
attr_accessible :attr_a, :attr_b, #name of attributes `attr_c` and `attr_d`
:attr_c, :attr_d #are equal to `method_c` and `method_d` names
#init some values
after_initialize :default_values
def default_values
self.is_active ||= true
self.attr_a ||= 'None'
self.attr_b ||= 1
if !self.respond_to?("method_c")
#return something if the method is called
self.method_c = 'None' #not working
end
if !self.respond_to?("method_d")
#return something if the method is called
self.method_d = 'None' #not working
end
end
#more methods
end
但是我在规范测试中遇到错误:
NoMethodError:
undefined method `method_c' for #<Object:0xbb9e53c>
我知道这听起来很疯狂,但是如果该方法不存在,我该怎么做才能返回一些东西?