Forum: Ruby on Rails Injecting Ruby code into a javascript file?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-04-15 21:33
Hi,

as far as I know it's possible to inject Ruby code into javascript code
if the javascript code is included in a view/page.

But is this also possible when the javascript code is in a separate .js
file that is located in the folder "public/javascripts"?

Thanks for your help!
Tom
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-04-15 21:43
(Received via mailing list)
> as far as I know it's possible to inject Ruby code into javascript
> code
> if the javascript code is included in a view/page.
>
> But is this also possible when the javascript code is in a
> separate .js
> file that is located in the folder "public/javascripts"?

No.  The files in public/javascripts never go through Rails.  You
could always create a controller/view to generate JS files though and
then treat them similarly.

Or consider setting some initial JS objects or JSON strings that your
JS methods can look at when doing their thing.
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-04-15 22:23
Philip Hallstrom wrote:
[...]
> Or consider setting some initial JS objects or JSON strings that your
> JS methods can look at when doing their thing.

If you do it this way, the browser can cache the JS and not hammer your
server.  I highly recommend this approach rather than dynamically
generating JS.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-04-16 18:00
Thanks a lot!
Tom
F999a7c806afd4b1d53f9fed9a18c704?d=identicon&s=25 Jason Arora (Guest)
on 2009-04-17 01:17
(Received via mailing list)
I think this is what you are looking for:

generate dynamic javascripts in the javascripts/ folder:
http://railscasts.com/episodes/88-dynamic-select-menus

You can cache the JS with standard Rails caching mechanisms. Here is
how:
http://railscasts.com/episodes/89-page-caching
82b09b041448e75d040587fa76f240c1?d=identicon&s=25 Shaun Keller (akira)
on 2009-04-17 10:13
Philip Hallstrom wrote:
>> as far as I know it's possible to inject Ruby code into javascript
>> code
>> if the javascript code is included in a view/page.
>>
>> But is this also possible when the javascript code is in a
>> separate .js
>> file that is located in the folder "public/javascripts"?
>
> No.  The files in public/javascripts never go through Rails.  You
> could always create a controller/view to generate JS files though and
> then treat them similarly.
>
> Or consider setting some initial JS objects or JSON strings that your
> JS methods can look at when doing their thing.

Alternatively, assign the ruby code to a JS variable in the appropriate
view/page, then you can use this variable freely in the
public/javascripts script.

Just make sure it is assigned before it will be used
This topic is locked and can not be replied to.