Forum: Ruby on Rails javascript file structure

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.
595e883f693a47f19867644966a15163?d=identicon&s=25 Vladimir Prieto (vladimirprieto)
on 2009-03-03 02:40
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...
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-03 02:54
(Received via mailing list)
On Mar 3, 1:40 am, Vladimir Prieto <rails-mailing-l...@andreas-s.net>
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
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-03-03 05:15
Or is this way correct?

In your helper code like

def  js_to_be_included
   %{
       <script>
           function test_function{
                     -------
                }
                </script>
    }
end

       Now in your view include this
<%= js_to_be_included %>

      Now call it  like test_function

Sijo
This topic is locked and can not be replied to.