简体   繁体   English

Rails ajax post from按钮从请求中的表单外部发送值

[英]Rails ajax post from button that sends values from outside of the form in the request

I'm somewhat new to rails and am wrapping a GUI around a CLI tool to give some non-technical users some self-service. 我对Rails有点陌生,并且正在将GUI包裹在CLI工具周围,以为一些非技术用户提供一些自助服务。 Part of the tool contains a page where the user selects a few values from some drop downs etc, and then clicks one of a few buttons on the screen to kick off their desired actions. 该工具的一部分包含一个页面,用户可以在其中从一些下拉菜单等中选择一些值,然后单击屏幕上的几个按钮之一以启动所需的操作。 The fields where they make their selections are outside of the forms that the buttons belong to. 他们进行选择的字段不在按钮所属的表单之外。 On button click, I want to call a javascript function that will grab the values the user selected, and post an ajax call back to the server to run the command corresponding to the button, and then when the request returns I want to insert the command output into a text area. 在单击按钮时,我想调用一个javascript函数,该函数将获取用户选择的值,然后将ajax调用发布回服务器以运行与该按钮相对应的命令,然后在请求返回时我想插入该命令输出到文本区域。

The only problem I'm having is with getting the values of the fields outside of the form generated by button to, and including them in the ajax request. 我唯一的问题是获取按钮生成的表单之外的字段的值,并将其包含在ajax请求中。 Outside of rails this is pretty trivial, but I'm having a hard time getting this to work. 在Rails之外,这是微不足道的,但是我很难使它起作用。 Does anybody have any suggestions or examples? 有人有任何建议或示例吗?

Thanks 谢谢

There's really no need for what I was trying to do... it ends up I can just use the form_tag helper and multiple submit_tag helpers to create the submit buttons with different values. 确实不需要我要做的事情……最终,我可以只使用form_tag帮助器和多个Submit_tag帮助器来创建具有不同值的提交按钮。 See this post for details: How do I create multiple submit buttons for the same form in Rails? 有关详细信息,请参见这篇文章: 如何在Rails中为同一表单创建多个提交按钮?

Also, here is a great write up on the javascript portion (it uses coffee script, but I didn't have any trouble following it even though I'm using jquery): http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html 另外,这是关于javascript部分的出色文章(它使用了咖啡脚本,但是即使使用jquery,我也没遇到任何麻烦): http : //edgeguides.rubyonrails.org/working_with_javascript_in_rails.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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