鞋子 Rb 主机名未知:sub.localhost:3000

Shoes Rb Hostname not known: sub.localhost:3000

我正在使用鞋子的 GUI 构建器,当我向本地 Rails 服务器

发出简单的 HTTP 请求时,出现以下错误

所以当我提出请求时

res = Net::HTTP.get(URI.parse("http://sub.localhost:3000"))

我收到错误:

Hostname not known: sub.localhost
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:12:in `rescue in getaddress'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:9:in `getaddress'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:23:in `initialize'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:879:in `open'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/timeout.rb:73:in `timeout'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:878:in `connect'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:852:in `start'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:1375:in `request'
shoes.rb:24:in `request'
shoes.rb:63:in `block (3 levels) in <main>'
-e:1:in `call'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:12:in `rescue in getaddress'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:9:in `getaddress'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/resolv-replace.rb:23:in `initialize'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:879:in `open'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/timeout.rb:73:in `timeout'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:878:in `connect'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:852:in `start'
/Applications/Shoes.app/Contents/MacOS/lib/ruby/2.2.0/net/http.rb:1375:in `request'
shoes.rb:24:in `request'
shoes.rb:63:in `block (3 levels) in <main>'
-e:1:in `call'

我的 hosts 文件有 127.0.0.1 sub.localhost,我可以到达该地址并对它发出请求没问题,只是不在 shoes 项目中。

我按照问题的建议绑定了我的本地IP地址ex 123.12.12.1,但是得到了同样的错误Hostname not known: sub.123.12.12.1

This 说要把shoes项目里的那行resolv-replace.rb:9:in去掉,但是我得到同样的错误Hostname not known: sub.123.12.12.1

我在 Mac

使用 custom DNS resolver 确保 Ruby 查看 hosts 文件:

require 'resolv-replace'

hosts_resolver = Resolv::Hosts.new('custom_hosts')
dns_resolver = Resolv::DNS.new

Resolv::DefaultResolver.replace_resolvers([hosts_resolver, dns_resolver])

require "net/http"
require "uri"

res = Net::HTTP.get(URI.parse("http://sub.localhost:3000"))

现在 Net::HTTP 将使用您的自定义解析器首先检查 hosts 并成功解析您的自定义域。