我对 Rails 很陌生,我有一个像这样从控制台运行的脚本
$ ruby axml2xml.rb ExamPaper.apk
现在,如何从我的控制器方法中调用此脚本并传递与ExamPaper.apk
?
我试过require 'axml2xml.rb'
了,但有一些指向这行代码的错误Zip::ZipFile.foreach(ARGV[0]) do |f|
。所以基本上,我如何axml2xml.rb 'ExamPaper.apk'
在我的控制器中制作类似的东西?
我对 Rails 很陌生,我有一个像这样从控制台运行的脚本
$ ruby axml2xml.rb ExamPaper.apk
现在,如何从我的控制器方法中调用此脚本并传递与ExamPaper.apk
?
我试过require 'axml2xml.rb'
了,但有一些指向这行代码的错误Zip::ZipFile.foreach(ARGV[0]) do |f|
。所以基本上,我如何axml2xml.rb 'ExamPaper.apk'
在我的控制器中制作类似的东西?
You can try using system
or popen
, but only for short tasks, for more information about that, please see here.
If your task is more time consuming you definitely should have a look at something like delayed_job and use a background job or some sort of queue to run your job. This way your server doesn't get blocked and your users do not have to wait til your job completes.
如果要将其作为 shell 命令执行,请使用:
exec 'ruby axml2xml.rb ExamPaper.apk'
在 ruby 中,有几种方法可以运行 shell 命令。
system("ls")
%x("ls")
`ls` #nice they are back ticks
exec "ls"
But I'm not sure about the permissions necessary for running commands like that via rails.