JST undefined for rails 3.1 application

痞子三分冷 提交于 2019-12-19 05:23:43

问题


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?


回答1:


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.




回答2:


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"



回答3:


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.




回答4:


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.




回答5:


It’s seems that JST has nothing to do with Backbone or Underscore, it’s done by back-end and will result the JST object and JST function in your front-end pages.



来源:https://stackoverflow.com/questions/8396051/jst-undefined-for-rails-3-1-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!