Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JST undefined for rails 3.1 application

Most rails 3.1 tutorials regarding the asset pipeline and javascript templating lead me to believe that the asset pipeline will pick up any *.jst files and slap them into a JST variable that's available to your *.js files. However, I'm currently stumped with the following error when I try to load a *.jst template:

Uncaught ReferenceError: JST is not defined

Any pointers?

like image 466
kelly.dunn Avatar asked Dec 06 '11 06:12

kelly.dunn


4 Answers

if you are using a require line like

//= require_tree ../templates

make sure that line is above the line that includes whatever file is giving you the error.

like image 131
whatbird Avatar answered Oct 23 '22 17:10

whatbird


The JST variable seems to get set if you correctly include the javascript template items in your app/assets/application.js file so they can be included via the asset pipeline:

//= require templates/your_template.jst

Then include the javascript template in your corresponding rails views (using haml):

- content_for :javascripts do
  = javascript_include_tag "templates/your_template"
like image 40
kelly.dunn Avatar answered Oct 23 '22 16:10

kelly.dunn


All the other answers are getting at this, but to clarify...

JST is not defined by sprockets unless one or more .jst files have been required in the manifest.

So even if you have //= require_tree ../templates in your manifest, JST will still be undefined until you create at least one .jst file in the templates directory.

Also be sure to include the EJS gem. If you've included backbone-on-rails, you have it already.

like image 24
tybro0103 Avatar answered Oct 23 '22 16:10

tybro0103


Sounds like you need the EJS Gem, which is included if you use the rails-backbone Gem.

Then you just create a file like app/asset/javascripts/foobar.jst.ejs and it can be rendered by calling

JST['foobar']()

Hope that answers your question.

like image 33
user210977 Avatar answered Oct 23 '22 15:10

user210977