Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在我的RubyonRails网站上运行Ruby脚本?_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何在我的RubyonRails网站上运行Ruby脚本?

Ruby on rails 如何在我的RubyonRails网站上运行Ruby脚本?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,我有一个ruby脚本(example.rb),我想在用户点击RubyonRails网站上的按钮时运行它!要做到这一点,有哪些步骤 谢谢首先,您需要有一个在单击按钮时调用的操作。然后在操作中,您可以使用%x调用脚本。例如: def my_action result = %x(ruby example.rb) end 如果要在rails应用程序的上下文中运行脚本,则需要使用rails runner def my_action result = %x(bin/rails runner exa

我有一个ruby脚本(example.rb),我想在用户点击RubyonRails网站上的按钮时运行它!要做到这一点,有哪些步骤


谢谢

首先,您需要有一个在单击按钮时调用的操作。然后在操作中,您可以使用%x调用脚本。例如:

def my_action
  result = %x(ruby example.rb)
end
如果要在rails应用程序的上下文中运行脚本,则需要使用rails runner

def my_action
  result = %x(bin/rails runner example.rb)
end

你可以找到更多关于在ruby中执行shell命令的有用信息。

这个问题很老了,但仍然会出现在google搜索的第一页,所以这里是我使用的

该命令将在rails应用程序的上下文中执行该文件

rails runner [path to file].rb

Edit:在使用了几次之后,我发现您可以在命令的开头添加
RAILS\u ENV=production
,以指定运行脚本的环境。

您可以与我们分享脚本必须执行的操作吗?脚本很长!!!但最终它将启动一个EC2实例!这还不够,还是你需要更多的信息?