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
%>
Scott Swezey
  • 2,147
  • 2
  • 18
  • 28
  • 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 %> – 244an Feb 27 '13 at 01: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. – Scott Swezey Feb 27 '13 at 01:39

2 Answers2

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.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • You say it doesn't belong in "HTML", I guess you mean `body`. See https://stackoverflow.com/questions/436411/where-should-i-put-script-tags-in-html-markup for dissenting opinions where to put javascript tags. – Christopher Oezbek Oct 05 '21 at 08: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 ` – Marnen Laibow-Koser Oct 06 '21 at 02: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

Community
  • 1
  • 1
  • 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." – Scott Swezey Feb 26 '13 at 21:56
  • Downvoting: there's no reason to use JS partials when you can just have a ` – Marnen Laibow-Koser Jun 03 '14 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. – Marnen Laibow-Koser Jun 06 '14 at 16:20