1

I have some pages in my Rails application that need one off bits of javascript to be included, ideally just before the </body> tag. There is no real need to have this javascript included on EVERY page since most don't use it. I've found a way to make this work, but I think the code is terrible.

How would you do the same thing or how would you refactor the existing code?

View simplified, sample code on gist.github.com: https://gist.github.com/scottswezey/ffc7bf52041b976b710a

(Or see the same code below:)

application.html.erb (Layout):

<!DOCTYPE html>
<html lang="en">
  <head>
  ...
  </head>

  <body>
    ...
    <script>
    $(function() {
      <%= yield(:js) %>
    });
  </script>
</body>
</html>

some_view_file.html.erb (View):

<%
str = <<END_OF_STRING
$('.modal').modal()
END_OF_STRING

content_for :js do
  str.html_safe
end
%>
2
  • Don't know if I understand your question, but you can set a variable (e.g. @js_code) in the action in the controller, and use <% if @js_code %> <script> ... in application.html.erb?
    – 244an
    Feb 27, 2013 at 1:06
  • I'm not trying to selectively display the js code, I'm trying to put snippets of code into view files and have them included in the layout with content_for. Feb 27, 2013 at 1:39

2 Answers 2

1

Don't ever do it this way: JavaScript doesn't belong in HTML. Just put an appropriate <script> tag in the page, referring to an external JS file, something like this:

application.html.haml

!!!
%html
  %head
    = yield :javascript
  %body
    = yield

view file

- content_for :javascript do
  = javascript_include_tag 'modal'

app/assets/modal.js

$(document).ready(function() {
  $('.modal').modal();
}

This keeps everything nicely separated.

2
  • You say it doesn't belong in "HTML", I guess you mean body. See stackoverflow.com/questions/436411/… for dissenting opinions where to put javascript tags. Oct 5, 2021 at 8:11
  • @Christopher I meant that JS code doesn’t belong anywhere in an HTML document. It should be included from an external JS file with <script src='modal.js'>, as I did here. Oct 6, 2021 at 2:31
-1

Is it possible to add a .js.erb partial with your JS and render it under the body?

edit: Check out this answer: https://stackoverflow.com/a/10113547/1283742

3
  • My initial intent had been to keep this helper code next to the view code (HTML) that it was powering. I'm not sure a separate partial is the best solution, but it did get me thinking that I should look into the asset pipeline for this. Still not my ideal, but maybe "better." Feb 26, 2013 at 21:56
  • Downvoting: there's no reason to use JS partials when you can just have a <script src='something.js'> tag, and no reason to use .js.erb when you can manipulate the DOM instead. Jun 3, 2014 at 18:23
  • @ScottS. No HTML document should contain JavaScript, so keeping it adjacent to the HTML isn't a good idea. Rather, keep the reference to the external JS file near the HTML if you want to. Jun 6, 2014 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.