views:

54

answers:

3

I am new to Rails and I have a pretty simple problem with calling javascript functions from within a view. In Rails 2 I would do...

= javascript_tag "name(arguments)"

where the javascript function "name" was located in my application.js file. However, this does not appear to work in Rails 3? Or am I missing something? I have been searching Google for some time without finding an answer.

UPDATE:

OK, so I looked at the source of the two different ways (using the javascript_tag and the haml javascript filter) as suggested. And this is very strange because the html source appears to be identical? Apart from a difference in double and single quotes in declaring the script type.

FIRST: using the javascript_tag which does not work

= javascript_tag "number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}'"

Source...

<div id='number_number_interval_727'>loading</div>
<script type="text/javascript">
//<![CDATA[
number_interval(6952596670.36814, 2.33002440293917, 0, 'number_number_interval_727'
//]]>
</script>

SECOND: using the haml javascript filter and it works

:javascript
  number_interval(#{fact.current_value}, #{fact.growth_per_second}, #{fact.decimal_number}, '#{dom_id(fact, "number")}')

Source...

<div id='number_number_interval_727'>loading</div>
<script type='text/javascript'>
//<![CDATA[
number_interval(6952596917.02179, 2.33002440293917, 0, 'number_number_interval_727')
//]]>
</script>

Well, I guess I'll just stick with the haml filter!

A: 

Check out the right syntax on link text

and Check if you have included the javascript defaults in your file.As this working for me in RAILS 3 also

Hitesh Manchanda
The syntax is correct and javascript defaults are included. The javascript_tag "alert('All is good')" also works, but if I for example put "alert('all is good')" into a function of its own, place it in application.js, and then try to call it with javascript_tag it does not. But maybe this is not the way to do it.
kbjerring
Well then you have an error in your function or syntax. Post your function code, exactly as you have it.
Squeegy
A: 

A friend of mine pointed me to the fact that there is a javascript helper in haml. Apparently, I can call javascript functions by using...

:javascript
  name_of_function(arguments)

This works, but of course only with haml.

kbjerring
A: 

Silly question but... is application.js included in your layout?

One option to try is to hand-code the js function-call into your view in "real" javascript - just to see if it works. eg

<script type="text/javascript">
  name(arguments)
</script>

Then you can be sure it's not the js itself (or lack thereof) at fault.

Taryn East
Yes, the application.js is included. I guess the :javascript filter in haml does what you suggest,...and it works.
kbjerring
Ok... so the next option is to view-source on what is put into the html when you do the one that's breaking... and compare that to what's int he source when you use the haml-version.if you can copy the relevant source-code into your question (above) we can disect it and see what's going on.
Taryn East