简体   繁体   中英

why rails app need to require 'net/http' after deploy to a new server

  1. AWS centos7
  2. rvm install ruby 2.3.3/2.4.0
  3. rails 5.0.1/5.0.2

It needs to call net/http in model, and it works well on AWS centos7, ruby 2.3.3, rails 5.0.1. But after deploy the environment as above, it raise a error

NameError (uninitialized constant Net::HTTP)

so I need to

require 'net/http'

in the model

Why? What's change caused it?

I ran into the same issue as you describe above, both on CentOS and on MacOS. I was able to resolve this by reinstalling an older version of bundler: (I tried this on both Ruby 2.4.0 and 2.4.1)

Here's what my testing showed, starting in your Rails project directory.

rails c

Loading development environment (Rails 5.0.2)
2.4.1 :001 > Bundler::VERSION
 => "1.14.6" 
2.4.1 :002 > url = URI.parse('https://www.google.com')
 => #<URI::HTTPS https://www.google.com> 
2.4.1 :003 > http = Net::HTTP.new(url.host, url.port)
NameError: uninitialized constant Net::HTTP
  from (irb):3
  from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.2/lib/rails/commands/console.rb:65:in `start'
  from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.2/lib/rails/commands/console_helper.rb:9:in `start'
  from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:78:in `console'
  from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
  from /usr/local/rvm/gems/ruby-2.4.1/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
  from bin/rails:4:in `require'
  from bin/rails:4:in `<main>'
2.4.1 :004 > exit

I then uninstall all versions of bundler:

gem uninstall bundler

And reinstalled the older version of bundler:

gem install bundler --no-document -v=1.14.3

rails c

Loading development environment (Rails 5.0.2)
2.4.1 :001 > Bundler::VERSION
 => "1.14.3" 
2.4.1 :002 > url = URI.parse('https://www.google.com')
 => #<URI::HTTPS https://www.google.com> 
2.4.1 :003 > http = Net::HTTP.new(url.host, url.port)
 => #<Net::HTTP www.google.com:443 open=false> 
2.4.1 :004 > exit

It looks like there's an issue with bundler 1.14.6 that is fixed by downgrading to 1.14.3.

Hope that helps!

edit: I also posted an issue for this in the Bundler GitHub Issues

edit2: Looks like this change is intentional and your application should include require 'net/http' itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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