我正在尝试将变量名称添加到rails中的method_name中。我收到了错误。
**Controller ACTION** ===================== def my_action(state) method_#{state} end **Model methods** ==================== def method_start end def method_end end
如何调用带有变量名的方法我没有得到。
答案 0 :(得分:2)
使用Object.send
按名称调用方法。例如,
def my_action(state)
if [:start, :end].include?(state)
model.send("method_#{state}")
end
end
请务必验证state
变量的安全性。 Object.send
可以调用任何方法,包括私有方法。