# Dynamic javascript

**URL:** <https://www.ruby-forum.com/t/dynamic-javascript/193895>\
**Category:** Rails\
**Created:** [August 26, 2010, 10:04am UTC](https://www.ruby-forum.com/t/dynamic-javascript/193895 "2010-08-26T10:04:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CUCUCU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@CUCUCU](https://www.ruby-forum.com/u/CUCUCU)\
**Post date:** [August 26, 2010, 10:04am UTC](https://www.ruby-forum.com/t/dynamic-javascript/193895/1 "2010-08-26T10:04:29Z")

</div>

I’m coming from a long period of CakePHP development. One of the more  
useful features of that framework was the ability to add any inline  
javascript in a view, with dynamic php codeblock-generated output  
(variables, framework generated URLs, etc), to a javascript buffer,  
and then have CakePHP render it inside an inline script tag just  
before the , or, my preferred solution, have CakePHP  
automatically condense the buffer and output it to a uniquely-  
named .js file on the filesystem. This file could then obviously be  
cached by the browser, and was recreated whenever the structure of the  
view/javascript changed.

Is there a similar feature in Rails, or a Gem that offers this  
functionality? Otherwise, what would be the best practice in Rails for  
unavoidable inline javascript output?

Many thanks

---

<div class="post-metadata">

**Author:** ![CUCUCU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@CUCUCU](https://www.ruby-forum.com/u/CUCUCU)\
**Post date:** [August 26, 2010, 1:53pm UTC](https://www.ruby-forum.com/t/dynamic-javascript/193895/2 "2010-08-26T13:53:30Z")

</div>

CU wrote:

> I’m coming from a long period of CakePHP development. One of the more  
> useful features of that framework was the ability to add any inline  
> javascript in a view, with dynamic php codeblock-generated output  
> (variables, framework generated URLs, etc), to a javascript buffer,  
> and then have CakePHP render it inside an inline script tag just  
> before the , or, my preferred solution, have CakePHP  
> automatically condense the buffer and output it to a uniquely-  
> named .js file on the filesystem. This file could then obviously be  
> cached by the browser, and was recreated whenever the structure of the  
> view/javascript changed.
> 
> Is there a similar feature in Rails, or a Gem that offers this  
> functionality?

Well, there’s content\_for, for starters. I think Rails provides some  
other methods for combining JS, but I’m not certain.

> Otherwise, what would be the best practice in Rails for  
> unavoidable inline javascript output?

Just don’t write inline JS to start with!

Rails 3 is more helpful in this respect than Rails 2, or so I’m told.

> Many thanks

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![CUCUCU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@CUCUCU](https://www.ruby-forum.com/u/CUCUCU)\
**Post date:** [August 26, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/dynamic-javascript/193895/3 "2010-08-26T14:53:07Z")

</div>

I thought of content\_for literally 15 seconds after clicking send on  
this post 🙂

I’m using Rails3, and you’re right, there is a heavy focus on moving  
away from inline JS. Perhaps I should move away from Cake’s bad  
practices, rather than try to find equivalents for them in Rails 🙂

Thanks for the help!
