Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

run ruby file from ajax or on button click event

i am new in ruby. so i have html code which includes one input field and button and i want when user click on that button i have to run one ruby file which has system() method.

index.html


     <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     </head>
     <body>
        <form method="post" action="/runMethod">
           <input type="text" name="name" value="whatever">
           <input class='btn btn-primary' type='submit' value='click'>
        </form>
     </body>

   

app.rb

 system("wayback_machine_downloader userInput")

this is my two files code. i just want that if i click on button app.rb file should run with given input. is it possible?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Technically, you can by writing JS code which executes the ruby file as a script by first using shell, and given the computer has ruby installed you can simply run something like this:

var w = new ActiveXObject("WScript.Shell");
w.run('ruby Scripts\test.rb');

This is however not a good way to do it as you don't get any response or error messages from the executed file, it might cause some security issues and you're executing business logic in the frontend, which is always bad.

The correct way to do it is to make the ajax call to a endpoint, where you can control the access to the information, set business logic in the backend and give proper response. I'm not sure if you're using rails, but if you're you should just create a new path in routes.rb and create a controller method for it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...