I’m kinda new to using Rails, and an app I am working on is progressing well – however I’m looking though the generated HTML and noticed things like…
<script type='text/javascript'> //<![CDATA[ Droppables.add(...); //]]> </script>
Sprinkled around the HTML, which of course matches up with places where I use:
<%= drop_receiving_element ... %>
What I’m wondering is… is there a better way to do this, or make it cleaner? Some of these script tags are coming from partials so putting them at the ‘bottom of the page’ doesn’t really help in this situation.
Another option may be pushing all these ‘tag blocks’ into an array then writing them out in the application.rhtml file, but its still a bit messy…
Well if you really want to use best practices…Do not use inline javascript. Keep your HTML, CSS and Javascript clean and seperated from eachother. Ideally the html file should be usable without CSS and javascript.
The cleanest way, imo, is to just build your app using plain html/css, and enhance it with unobtrusive javascript to provide a better user experience.
A pattern for this is to include all your JS files at the bottom of your page, and start running your code with functionality like onDomReady.
Based on the comments I would like to add some possible options to get you started: