简体   繁体   English

当目标脚本被另一个Ruby脚本生成时找不到Ruby gem

[英]Ruby gem not found when target script is spawned by another ruby script

I've a watir script that I want to fire off from a Rails app on demand. 我有一个watir脚本,我想按需从Rails应用程序启动。 My controller action looks something like this: 我的控制器动作如下所示:

def run_scrape
  pid = Process.spawn('C:\Ruby22-x64\bin\ruby.exe "C:\Users\Admin\Dropbox\dev\watir-scripts\ds-vin.rb" 3VWB07AJ')
  Process.detach(pid)
  true
end

The actual script starts with: 实际的脚本以以下内容开头:

require 'watir-webdriver'
b = Watir::Browser.new :ie
# etc

Now, if I navigate to the folder and just run the script directly, everything works fine. 现在,如果我导航到该文件夹​​并直接运行脚本,则一切正常。 Once it's triggered from Rails (which is at a different location; seems like that's messing it up) I'm greeted with the following error: 一旦它从Rails触发(位于其他位置;似乎把它弄乱了),我会遇到以下错误:

`require': cannot load such file -- watir-webdriver (LoadError) `require':无法加载此类文件-watir-webdriver(LoadError)

Watir-webdriver is in my gem list. Watir-webdriver在我的宝石清单中。 I'm also requiring a couple of .rb files in the same folder, which was a problem until I used require_relative . 我还要求在同一文件夹中有几个.rb文件,这在使用require_relative之前是一个问题。 I'm missing something simple here, I think. 我想这里缺少一些简单的东西。 How do I require_relative a global gem? 我如何require_relative全球性宝石?

First solution is to run your scraper in the same process as Rails, but in a different thread: 第一种解决方案是在与Rails相同的过程中运行您的scraper,但使用不同的线程:

require 'C:\Users\Admin\Dropbox\dev\watir-scripts\ds-vin.rb'
thr = Thread.new do 
    b = Watir::Browser.new :ie
    ...
end

Second is to fix your environment if you are going to use Process.spawn - assuming your gems are installed system-wide you can do something like 其次,如果要使用Process.spawn ,请修复您的环境-假设您的gem在系统范围内安装,则可以执行以下操作

ENV['GEM_PATH'] = "c:/Ruby220/lib/ruby/gems/2.2.0"

at the top of your ds-vin.rb script. ds-vin.rb脚本的顶部。 To find the exact gem path you should use, make sure you run gem which watir-webdriver and gem environment and copy from there. 要找到您应该使用的确切gem路径,请确保您运行gem which watir-webdrivergem environment并从那里复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM