16

I want to include some Javascript functionality in a Haml view, but I don't want it inserted into every view. Do I have to put the Javascript file in public/javascripts or can I put it in app/assets/javascripts to keep it hidden from user, and still reference from within the haml view file?

How would you do both these options if they are possible?

Leila Hamon
  • 2,505
  • 7
  • 24
  • 32

3 Answers3

31

You should just use

!!!
%html
  %head
    = javascript_include_tag "my_js_file"

if it's specific to one place, you should use content_for

!!!
%html
  %head
    = yield(:javascripts)

And then in your view

- content_for :javascripts do
  = javascript_include_tag "my_js_file"
Draiken
  • 3,805
  • 2
  • 30
  • 48
11

Include Directly

If you want the javascript included directly into the haml, you can use :javascript

:javascript
  $(function() { alert("js inside haml"); }

You can put this into a partial and then just render the partial to keep your views clean.

Reference It

If you want to just reference javascript and have the browser pull it in, you should use javascript_include_tag like always. Here, you'll need to make the javascript file a manifest, instead of requiring it into the application.js manifest. Remember to add the manifest to config.assets.precompile in your application.rb, according to http://guides.rubyonrails.org/asset_pipeline.html

(in your haml):

= javascript_include_tag 'somefile'

(in config/application.rb):

config.assets.precompile += ['somefile.js']
Ben Taitelbaum
  • 7,343
  • 3
  • 25
  • 45
  • This is very helpful as well, thank you for taking the time to respond. – Leila Hamon Jul 12 '12 at 16:10
  • Is this the more modern of the two solutions? You said the phrase 'asset pipeline' so I am moved to believe that it is. – Ziggy Mar 29 '13 at 19:49
  • @Ziggy the other solution also provides a way to include certain javascript files (in the header) by modifying the template. – Ryan Taylor Jul 03 '13 at 22:04
  • @ben-taitelbaum Can you explain what you meant by `Here, you'll need to make the javascript file a manifest, instead of requiring it into the application.js manifest` ? – Sandip Subedi Nov 06 '17 at 19:22
  • @SandipSubedi, you'd be fine putting the javascript into a file that's included into application.js as normal if it defines a function that you call from the page, but if you want to do `javascript_include_tag :some_other_file` then you create another _manifest_ that's similar to `application.js` in terms of how it works. See http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives for more details. – Ben Taitelbaum Nov 08 '17 at 00:28
0

If your javascript is small and dealy simple, I would suggest include javascript directly in the HAML:

:javascript
  alert('hi hi!')

Otherwise you should use asset pipeline. It makes sure that your javascripts are pre-processed, compressed and minified. It also helps to keep your javascripts well organized (e.g. separation between your scripts and vendor scripts) and easily testable (with testing frameworks like jasmine/jasminerice/evergreen). If you are new to asset pipeline, here is a good read =)

Wei
  • 1,252
  • 13
  • 19