开发者

How to add dynamically generated inline javascript using Rails 3.1 and Haml

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)}'
...


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} );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜