使用 Sinatra 和 ruby​​ 连接 HTML 按钮

发布于 2024-08-20 01:16:21 字数 395 浏览 1 评论 0原文

披露:我对网络编程一无所知

问题背景: 我有一个环境测试室在不同温度下测试嵌入式计算机。它由 Windows 应用程序控制。我可以通过 ruby​​ 和控制应用程序的 Win32API 接口来控制腔室。该会议厅距离我的办公室很远,我可以远程监控状态并通过网络界面触发更改。 Sinatra 可以轻松地通过 Web 服务器显示状态。

我的问题:我想单击状态网页上的按钮并通过 Sinatra 和 Ruby 向应用程序发送命令,而无需切换到其他页面。我找不到任何类似的例子或信息。

答案:您可以使用 Ajax(如已接受的答案中所示),但我将使用帖子并重新加载页面。它简单得多,但不是那么顺利。

Disclosure: I know nothing about web programming

Background to problem:
I have an environmental testing chamber testing embedded computers at various temperatures. It is controlled by a windows application. I can control the chamber via ruby and the Win32API interface of the control application. The chamber is far from my office and I would remotely monitor the state and trigger a change via a Web interface. Sinatra makes it easy to show the state via a web server.

My problem: I want to click on a button on the status webpage and send a command via Sinatra and Ruby to the application without switching to a different page. I cant find any examples or information for something like that.

The Answer: You can use Ajax(as in the accepted answer) but I am going with post and reload the page. It is a lot simpler but not as smooth.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-08-27 01:16:21

您是否已经有一个可以 POST 到 sinatra 处理程序并执行您想要的操作的表单?

如果是这样,您可以使用 jQuery 轻松使其支持 ajax。

  1. 将 jquery.js 添加到您的公共目录中从布局中要求它

  2. 添加一些 js 以在单击按钮时发布到处理程序。

    // 假设您的表单有 id=myform
    // 定义提交表单时的回调#myform。
    $("#myform").submit(function() {
      //使用ajax将表单内容发布到/action
      $.post("/action", $("#myform").serialize(), 函数(结果){
        // 假设 result 是 html 中更新数据的字符串
        // 并假设您的数据位于具有 id 数据表的元素中
        $("#data-table").html(结果)
      });
      返回假; // 阻止表单正常提交
    });
    

Do you already have a form that POSTs to a sinatra handler and does what you want?

If so, you could easily make it ajaxy by using jQuery.

  1. Add jquery.js to your public directory & require it from the layout

  2. Add some js to post to your handler when your button is clicked.

    // assuming your form has id=myform
    // defines a callback on submission of the form #myform.
    $("#myform").submit(function() {
      //posts the contents of the form to /action using ajax
      $.post("/action", $("#myform").serialize(), function(result){
        // assuming result is a string of the updated data in html
        // and assuming that your data goes in an element with the id data-table
        $("#data-table").html(result)
      });
      return false; // prevents the form from submitting normally
    });
    

jquery docs for

勿挽旧人 2024-08-27 01:16:21

我想[...]通过(服务器)[...]发送命令,而不切换到不同的页面

这称为 Ajax,通常使用 XMLHttpRequest

I want to […] send a command via (the server) […] without switching to a different page

This is known as Ajax and is usually implemented using XMLHttpRequest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文