-2
是否有可能在控制器中的实例变量的exec ruby代码?Rails有可能吗?
实施例:
def something
@code = "redirect_to 'https://www.google.com/'"
exec(@code) // And then it would redirect.
end
是否有可能在控制器中的实例变量的exec ruby代码?Rails有可能吗?
实施例:
def something
@code = "redirect_to 'https://www.google.com/'"
exec(@code) // And then it would redirect.
end
不要这样做。 Rails是Ruby。在Ruby中,您可以使用eval方法在字符串内执行任何命令。当你开始真正进入它时,有class_eval。
@myvar = "puts 'SHOULD NOT HAVE DONE THIS!!'"
eval(@myvar) # SHOULD NOT HAVE DONE THIS!!
保持安全。
您能否详细说明您要实现的目标?可能有更好的方法来解决你的问题。 –
肯定与科迪在这里。如果你想要这样执行Ruby代码,很可能是你做错了。 –
http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-eval是的,这是可能的。我不知道这是一个好主意,但... – Travis