Forum: Ruby on Rails set a rails variable to the output of a javascript function?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-24 16:06
I have a javascript function that outputs a string.  In my rhtml view
file, i want  to set a rails variable to the output from this function?
Can anyone tell me how to set this up please?
599cd6de77619cbf533946e10167bd16?d=identicon&s=25 JAWspeak (Guest)
on 2007-07-25 07:33
(Received via mailing list)
Ajax should do it, right?

Do one of the ajax calls posting that variable back

On Jul 24, 10:06 am, Max Williams <>
773f753fdb771f6a53a5d74b190c400a?d=identicon&s=25 Rene Retz (Guest)
on 2007-09-27 04:32
(Received via mailing list)
Do you have links with examples?

Is therre another way to do that? I would like to do that inside a rails

D7c511ce5025d37b8c6bd9134e0f2bd9?d=identicon&s=25 Thorsten (Guest)
on 2007-09-27 13:52
(Received via mailing list)
Javascript is client-side, ruby/rails is server side. The only way for
the 2 to exchange values or anything else for that matter,
is by sending requests to and replying back, either by ajax or a
normal request. you cant run javascript in the view before it's sent
to the browser, as well as you cant run ruby in the browser, only on
the server side.
773f753fdb771f6a53a5d74b190c400a?d=identicon&s=25 Rene Retz (Guest)
on 2007-09-27 21:12
(Received via mailing list)

Thaks for response,

I understand.
But I am sending a view to browser from server with a javascript. This
javascript will have some actions interacting with the user and than
user send a new request to control I would like to send with a string
processed throw javascript.
The problem is that I don't kwon how to put string returned from a
javascript call function inside an Form_tag.
I would like something like it:

 <% form_tag :action => 'create', :mystring => do %>
    <%= submit_tag "Create" %>
 <% end %>

foo is my javascript object.

Thanks again!

813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-09-27 21:26
(Received via mailing list)
On 9/27/07, Rene Retz <> wrote:
> javascript call function inside an Form_tag.
> I would like something like it:
>  <% form_tag :action => 'create', :mystring => do %>
>     <%= submit_tag "Create" %>
>  <% end %>
> foo is my javascript object.

I think the simplest way to do something like this is to create a
hidden field in the form and have your javascript function put its
value into that field so it gets passed as part of the request. If
your function needs to be run just as the form is submitted, you can
hook it into the onsubmit event.
773f753fdb771f6a53a5d74b190c400a?d=identicon&s=25 Rene (Guest)
on 2007-09-29 04:57
(Received via mailing list)
Could you give me an example?
Db7238007950074e9e73b76a81910406?d=identicon&s=25 tonypm (Guest)
on 2007-10-02 09:18
(Received via mailing list)
1.  To write to a form field using javascript in the page use:

document.repair_form.repair_customer_id.value= 45

where in my case:  repair_form is the name of the form and customer_id
is the name of the field.

2.  Another way to update the server with a js value is to pass it via
a call to remote.  ie.  create a new ajax call passing in the js
variable you want to send.  eg.

<script type="text/javascript">
function update_customer(customer_id) {
  new Ajax.Request('/repair/change_customer/'+customer_id,
          {asynchronous:true, evalScripts:true})

var external_update_customer = this.update_customer

now I can call this from js code and the value I pass can be obtained
from the params[:id] on the server side.
ie.  update_customer(67) will cause an asynchronous call to
change_customer method in repair controller with params[:id] of 67.

I prefer doing it this way and then updating the page fields from the
server render :update response.  You can do server side checking that
way and update other things like error prompts etc.

Incidentally to directly update the field from the server response you
can do:
render :update do |page|

Also, in the js snippet, I have included   - var
external_update_customer = this.update_customer.  This allows me to
call this function from a popup window.  using the js opener method.
So a popup window can now communicate a value back to the server via
the parent window.  Useful to do popup look-ups.

Finally, the js SCRIPT was added to the page directly.   I am
currently playing with different ways of doing these sort of updates,
but if I decide to use this approach, it would make sense to build a
rails helper to create the js updater function.

This topic is locked and can not be replied to.