# Calling a commandline function using the onclick button

**URL:** <https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092>\
**Category:** Rails\
**Created:** [September 7, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092 "2009-09-07T21:44:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 7, 2009, 9:44pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/1 "2009-09-07T21:44:31Z")

</div>

Hello everyone,

I’ll start by describing what I’m doing and saying that I am a newbie.  
In my ROR (2.1.2) application, would like to send read data from a text  
field using the onclick tag to a python script I wrote.  
The problem I encounter is with the onclick tag. I used the system()  
function to call the python script, but it doesn’t function. The python  
script is only called when I reload the page and not when I click on the  
send button.  
Who can help me or tell me how to better call the python script once I  
click on the send button.  
I’ll be grateful for your help.

This is what my code looks like:

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 7, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/2 "2009-09-07T21:53:34Z")

</div>

On Mon, Sep 07, 2009 at 09:44:31PM +0200, Ngassa C. wrote:

> Who can help me or tell me how to better call the python script once I  
> &’) %\>" /\>

I have no idea if calling system() from within an ERb template should  
work  
or not, it seems like a pretty wild idea 😃

I would try making a controller action that the form submits to, and  
have  
the controller make the system call with the data. I haven’t tested that  
but  
I would give it a try if possible.

HTH

Matt

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 7, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/3 "2009-09-07T22:00:36Z")

</div>

On Sep 7, 8:44 pm, Ngassa C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> click on the send button.
> 
> --

I think you have a basic misunderstanding of how erb templates work.  
Anything in \<% or \<%= tags is evaluated when the template is rendered

- that is what rendering an erb template means. Furthermore, an  
onclick options can only be a chunk of javascript.  
As Matt says, you need to write a controller action that your form is  
submitted to. Furthermore, be careful of the data you receive - if the  
example you posted worked it would allow a user to run arbitrary shell  
commands on your server.

Fred

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 8, 2009, 1:59pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/4 "2009-09-08T13:59:20Z")

</div>

> I’ll try writing a controller action.

The idea with the controller action was really a good one.  
Now I can call the pythonscript without problem while clicking on a  
link, but it still cannot read from the form.  
How can I do this, or what should be written at the place of :msg =\>  
“$(‘txt\_message’).value”.  
This is what my code now looks like:

 \<%=link\_to\_remote 'send', :url =\> { :action =\> 'sendmsg', :controller =\> 'contacts', :partner =\> @partner, :msg =\> "$('txt\_message').value" } %\>

Carine

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 7, 2009, 10:19pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/5 "2009-09-07T22:19:42Z")

</div>

> As Matt says, you need to write a controller action that your form is  
> submitted to.

Ok, thanks for the quick reply.  
I’ll try writing a controller action.

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 9, 2009, 7:54am UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/6 "2009-09-09T07:54:12Z")

</div>

Thanks to you all,

I got my problem solved using the form\_tag and I could query the entry  
from the field using params[:myparameter] and use them in the controller  
action. Using :with also functioned.

Carine

---

<div class="post-metadata">

**Author:** ![pl\_helpme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/fbc32d/32.png) [@pl\_helpme](https://www.ruby-forum.com/u/pl_helpme)\
**Post date:** [September 8, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/calling-a-commandline-function-using-the-onclick-button/175092/7 "2009-09-08T16:21:33Z")

</div>

On 8 Sep 2009, at 12:59, Ngassa C. wrote:

> > I’ll try writing a controller action.
> 
> The idea with the controller action was really a good one.  
> Now I can call the pythonscript without problem while clicking on a  
> link, but it still cannot read from the form.  
> How can I do this, or what should be written at the place of :msg =\>  
> “$(‘txt\_message’).value”.

Read up about the :with option to link to remote (I wrote a bit about  
it a while ago:

> **[:with or :without you: link\_to\_remote's mysterious parameter - Space Vatican](https://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterious-parameter/)**
>
> One of the nice things about Rails is that it makes it really easy to get off the ground with Ajax, thanks to the link\_to\_remote helper (and all the …

)

Fred
