如何在ruby Shoes GUI应用程序中创建简单的http请求?

问题描述:

我正在尝试向运行在localhost:3000的本地rails应用程序发出一个简单的http请求。我的应用程序的代码非常处于原型阶段,因为我只是在尝试其他任何事情之前尝试获取原始功能。

I'm trying to make a simple http request to a local rails application running at localhost:3000. The code for my application is very much in prototype phase as I am only trying to get raw functionality before I try anything else.

rails应用程序只返回看起来像的JSON在解析之前

The rails app simply returns JSON that looks like this before being parsed

"{\"response\":\"foo is blank\"}"

这是我现在正在尝试的代码:

Here's the code I'm trying right now:

require 'net/http'

def get_response
  return Net::HTTP.get(URI.parse("http://localhost:3000"))
end

Shoes.app(:width => 280, :height => 350) do

  flow do
    stack do
      button "make http request and display results in an alert box" do
        x = get_response
        alert x
      end# of button
    end# of stack
  end# of flow
end

我也尝试过这个方法在Shoes.app内部阻止无济于事。执行时,这将创建它应该创建的所有元素,但是当我单击按钮时没有任何反应。

I've also tried to put the method inside the Shoes.app block to no avail. When executed, this creates all the elements that it should create, but nothing happens when I click the button.

我知道你不应该做任何事情实用的红宝石鞋,但我还是试着。感谢您的时间

I know you're not supposed to be able to do anything practical with ruby shoes, but I'm trying anyway. Thanks for your time

当我尝试运行您的代码时,我遇到的问题是,该应用无法在本地连接使用 localhost 。而不是使用 localhost ,而是使用计算机的本地IP地址。

The problem for me when I tried to run your code was that the Shoes app couldn't connect locally using localhost. Instead of using localhost, use your computer's local IP address.

所以只需更改:

Net :: HTTP.get(URI。解析(http:// localhost:3000))

到此:

Net :: HTTP.get(URI.parse(http://10.1.4.123:3000))

如果您的计算机的本地IP 10.1.4.123

if your computer's local IP is 10.1.4.123.

要查看确认的错误日志发生这种情况,将此行插入鞋子应用程序的顶部: Shoes.show_log

To see the error logs that confirm this is happening for you, insert this line at the top of your shoes app: Shoes.show_log

或简单地说在Mac上输入 cmd + / ctrl +

Or simply enter cmd+/ on a Mac or ctrl+/ in Windows(?) when you are running your *.rb file in Shoes.app

对我来说,显示的错误是:

For me the error that showed up was:

连接被拒绝 - 连接(2)为:: 1端口3000