Gem Rails 中的未初始化常量

Uninitialized constant in Rails from Gem

我是 rails 的新手,正在努力尝试在我的服务器上将 Rails 应用程序安装到 运行。在我的本地 Mac 上,一切正常,但是当我在 Ubuntu 服务器上 运行 时,出现 NameError (uninitialized constant Api::V1::TestController::Headless) 错误。我已经按照其他帖子中的建议更新了捆绑器和相关的 gems。我确定 Headless 在我的 gem 文件中,是最新的,并且安装正确。我在 Watir Webdriver 中使用 Headless。对于可能导致此错误的任何建议,我们将不胜感激。

Ruby版本:2.2.3

相关控制器:

class Api::V1::TestController < ApplicationController
  respond_to :json

  def index
    respond_with "test controller"
  end

  def create

    event_submit(params[:json_event])

    respond_to do |format|
      if ( @log.present? )
        format.json { render text: "Log: " + @log }
      else
        format.json { render text: "Error, no log" }
      end
    end

  end

  def nul_check(param)
    param ||= "none"
    return param
  end


  def event_submit(params)

    @log = ""

    #initialize the log
    @log = ""
    #set the browser that we will use to chrome for testing
    #use headless
    headless = Headless.new
    headless.start

    browser = Watir::browser.start 'www.google.com'

    @log += browser.title

  end

end

宝石文件:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

#Api gems
gem 'active_model_serializers'
gem 'deathbycaptcha'
gem 'rspec'
gem 'responders', '~> 2.0'


#Driver gems
gem 'watir-extensions-element-screenshot'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
gem 'headless'
gem 'nokogiri'
gem 'watir-webdriver'

group :development, :test do
  gem 'sqlite3'
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end


gem 'unicorn'

问题是 Rails 在 Api::V1::TestController 命名空间下查找 Headless 常量。

您应该使用 ::Headless(常量的绝对路径)。

原来是服务器所有权问题。在我的例子中,用户 rails 没有宝石的所有权,因为我在自己的帐户下拥有 运行 bundle install。 rails 用户的 chmod 解决了这个问题!