# Javascript talks to rails

**URL:** <https://www.ruby-forum.com/t/javascript-talks-to-rails/126653>\
**Category:** Rails\
**Created:** [January 21, 2008, 3:07am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653 "2008-01-21T03:07:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [January 21, 2008, 3:07am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653/1 "2008-01-21T03:07:36Z")

</div>

i am trying to do this:

var name =“Some Value”  
mygrid.getCombo(2).get(value);  
alert(encodeURIComponent(’\<%=Product.find\_by\_name(’+name+’)%\>’));

all the code is good, the problem is that var name seems that is not  
sendin

how can i send a javascript var to ruby??

Regards,

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [January 21, 2008, 4:25am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653/2 "2008-01-21T04:25:49Z")

</div>

Could you shed more light on what you’re trying to do?

I think you’ll need an observe\_field call perhaps:

[http://noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actionview/helpers/prototypehelper/observe\_field.html](http://noobkit.com/show/ruby/rails/rails-edge/actionpack-edge/actionview/helpers/prototypehelper/observe_field.html)

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [January 21, 2008, 6:16am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653/3 "2008-01-21T06:16:35Z")

</div>

Well i am working with a datagrid in javascript and when a value is  
inserted in a column, i need to pass this value to the controller  
dynamically.  
this is the reason i am trying to do this, to see if the value is being  
passed correct:  
var product = ‘value’;  
alert(encodeURIComponent(’\<%=Product.find\_by\_name(’+product+’)%\>’));

other way i have been trying is assigning the value to a session  
variable by ajax method.

var product = ‘value’;  
new Ajax.Request(’\<%= “/” + request.path\_parameters[“controller”] +  
“/setActualProduct/?pCode=” %\>’+product,{);

method: setActualProduct  
change a session variable value

The problem is that this session var is render the firsttime you enter  
the page  
\<%pActual=session[:pActual]%\>  
alert(’\<%=pActual%\>’);

maybe i will need some observer but to a var instead to a field…

any ideas?

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [January 21, 2008, 9:19am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653/4 "2008-01-21T09:19:02Z")

</div>

the answer is Ajax + RJS.

> var name =“Some Value”  
> mygrid.getCombo(2).get(value);  
> alert(encodeURIComponent(’\<%=Product.find\_by\_name(’+name+’)%\>’));

\<%= remote\_function(:url =\> {, :name =\> name}) %\>

def

render :update do |page|  
page.alert(Product.find\_by\_name(params[:name]).id)  
end

end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [January 22, 2008, 12:34am UTC](https://www.ruby-forum.com/t/javascript-talks-to-rails/126653/5 "2008-01-22T00:34:29Z")

</div>

Thanks Keynan, i am doing an alert(), but what i really need is to pass  
this value to javascript function.
