Set a rails variable to the output of a javascript function?

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?

Ajax should do it, right?

Do one of the ajax calls posting that variable back

On Jul 24, 10:06 am, Max W. [email protected]

Do you have links with examples?

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


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.


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!


On 9/27/07, Rene R. [email protected] 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.

Could you give me an example?

  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.

  1. 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.

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.