# Javascript file structure

**URL:** <https://www.ruby-forum.com/t/javascript-file-structure/161853>\
**Category:** Rails\
**Created:** [March 3, 2009, 2:40am UTC](https://www.ruby-forum.com/t/javascript-file-structure/161853 "2009-03-03T02:40:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vladimirprieto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f475e1/32.png) [@vladimirprieto](https://www.ruby-forum.com/u/vladimirprieto)\
**Post date:** [March 3, 2009, 2:40am UTC](https://www.ruby-forum.com/t/javascript-file-structure/161853/1 "2009-03-03T02:40:29Z")

</div>

hi

as far as i know (i’m new on ror) the file structure of javascript files  
are:

app/views/posts/…  
public/javascripts/\*.js

what i understand as a View is final step of the application, the thing  
that user see, let’s say…the screen. in this sense, javascript in  
some cases, are also -part of- the “view” for some situations.

so, i understand that public/javascript is for general JS code purposes,  
but in my case i need to build special JS files for every controller.  
even more, in some cases, i need to build JS files for every action  
file.

in order to mantain the things on its place, i began to build this  
structure:

app/views/posts/edit.html.erb  
app/views/posts/new.html.erb  
public/javascripts/posts/edit.js  
public/javascripts/posts/new.js

even when it works, i must say that i find a little disorder. cuz, as i  
said before, this JS code is more ralated to the “view”. so, i really  
think that this will be a better structure:

app/views/posts/edit.html.erb  
app/views/posts/new.html.erb  
app/views/posts/edit.js  
app/views/posts/new.js

Â¿is there an easy a way to make this structure?  
Â¿how and where i must include these js files?  
Â¿wich is the best rails practice for such a thing?

thanks…

---

<div class="post-metadata">

**Author:** ![vladimirprieto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f475e1/32.png) [@vladimirprieto](https://www.ruby-forum.com/u/vladimirprieto)\
**Post date:** [March 3, 2009, 2:54am UTC](https://www.ruby-forum.com/t/javascript-file-structure/161853/2 "2009-03-03T02:54:52Z")

</div>

On Mar 3, 1:40 am, Vladimir P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ¿is there an easy a way to make this structure?  
> ¿how and where i must include these js files?  
> ¿wich is the best rails practice for such a thing?

Rails doesn’t really do much for you here - it’s expecting js can be  
served out of the document root (ie the public folder)  
As far as I know you’re pretty much on your own here. One way would be  
to have a rake task or similar that consolidates all of those small js  
files into a single all.js file which the user’s browser can download  
and cache once and for all - only all.js would need to be in public/  
javascripts

Fred

---

<div class="post-metadata">

**Author:** ![vladimirprieto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f475e1/32.png) [@vladimirprieto](https://www.ruby-forum.com/u/vladimirprieto)\
**Post date:** [March 3, 2009, 5:15am UTC](https://www.ruby-forum.com/t/javascript-file-structure/161853/3 "2009-03-03T05:15:18Z")

</div>

Or is this way correct?

In your helper code like

def js\_to\_be\_included  
%{  
  
}  
end

```
   Now in your view include this

```

\<%= js\_to\_be\_included %\>

```
  Now call it like test_function

```

Sijo
