Javascript file structure


#1

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…


#2

On Mar 3, 1:40 am, Vladimir P. 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


#3

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