2

I am using Rails 3.1 and Haml. What is the best way to add inline javascript that is generated in something such as a form_for block. I tried the following:

In layouts/application.html.haml,

...
:javascript
  yield(:jstemplates)
...

And in my form_for block,

...
= content_for :jstemplates do
  = "var my_var='#{generate_template(form, :my_field)}'"
...

Instead, I had to do the following:

...
:javascript
  #{@my_javascript}
...

and

...
- @my_javascript = capture_haml do
  var my_var='#{generate_template(form,:my_field)}'
...

1 Answer 1

1

i usually have the following on the bottom of my application layout:

:javascript
  $(document).ready(function() {
    #{yield :javascript}
  });

when i have view-specific javascript i write it directly into the view like so:

- content_for :javascript do
  :plain
    doSomeFunkyStuffWithRailsParameters( #{@whatever.title} );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.