# Ajax, javascript\_tag and JS Var trouble, help nedded

**URL:** <https://www.ruby-forum.com/t/ajax-javascript-tag-and-js-var-trouble-help-nedded/172538>\
**Category:** Rails\
**Created:** [July 23, 2009, 12:36am UTC](https://www.ruby-forum.com/t/ajax-javascript-tag-and-js-var-trouble-help-nedded/172538 "2009-07-23T00:36:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 23, 2009, 12:36am UTC](https://www.ruby-forum.com/t/ajax-javascript-tag-and-js-var-trouble-help-nedded/172538/1 "2009-07-23T00:36:08Z")

</div>

Hello there,

I have a clients controller, and I want it to respond both ajax and html  
to the new action.

If the user uses the html action, the new partial is going to be  
rendered and all works with no problem.  
In oder hand, if a ajax request is submitted, everything renders ok (it  
will be rendered inside a div in the index page), but some JS functions  
don’t.

## I have this piece of code in my view:

## \<% templates = “var phone\_template=‘#{generate\_template(contact\_form, :phones, :partial =\> “/contacts/phones/create”)}’” -%\> \<%= javascript\_tag(templates) -%\>

What will generate a script tag in the html file (if is called normally,  
html request), or the result is passed to the page (using ajax), and it  
can be visualized using firebug.

This code (in the same view, on the botton):  
\<%= link\_to ‘Adicionar Telefone’, ‘#phone\_template’, :class =\>  
“add\_nested\_item”, :rel =\> “contact\_phones” %\>  
Calls a function (evnt: onclick, function: add\_nested\_item) that is  
declared in the application.js file.

I have narrow the error down, and I thing that (pretty sure) that the  
function “add\_nested\_itme” cant find the js var “phone\_template”.

What can be done to solve this problem.

Regards,

David S.

Ps: Any extra code will be given if needed.

Application.js =\> [http://pastie.org/555701](http://pastie.org/555701)  
Partial (view) =\> [http://pastie.org/555708](http://pastie.org/555708)  
Result of javascript\_tag =\> [http://pastie.org/555714](http://pastie.org/555714)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 24, 2009, 12:30am UTC](https://www.ruby-forum.com/t/ajax-javascript-tag-and-js-var-trouble-help-nedded/172538/2 "2009-07-24T00:30:23Z")

</div>

I have not solved my problem yet.

When the var ( that was sent by ajax ) arrives, it looks like that it  
get evaluated, but the function can’t find it.

Any ideas?

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 24, 2009, 2:00am UTC](https://www.ruby-forum.com/t/ajax-javascript-tag-and-js-var-trouble-help-nedded/172538/3 "2009-07-24T02:00:25Z")

</div>

Problem is solved.

> * * *
> 
> ## \<% templates = “var phone\_template=’#{generate\_template(contact\_form, :phones, :partial =\> “/contacts/phones/create”)}’” -%\> \<%= javascript\_tag(templates) -%\>

I have removed the key “var” to make it global, so functions of other  
scopes could see it.

David S.
