【发布时间】:2013-02-02 04:09:29
【问题描述】:
我花了很多时间研究 sprockets 和 tit 的源代码,试图弄清楚如何将变量/绑定传递给 Erb 评估上下文。这就是我想要做的:我需要提供一个 JS 文件,其内容会根据每个请求而改变。更改的部分取决于存储在数据库中的数据,因此需要通过 Rails 应用程序路由请求并需要传递变量/绑定。除此之外,JS 文件使用require 指令插入其他 JS 文件,因此需要使用 sprockets。
这是无效的代码 sn-p:
控制器文件:
def ever_changing_js
@foobars = Foobar.all
MyApp::Application.assets.instance_eval do
def foobars
@foobars
end
end
render :text => MyApp::Application.assets.find_asset('ever_changing.js').to_s, :content_type => "application/javascript"
end
ever_changeing.js:
//= require file1.js
//= require file2.js
// Some code that uses @foobars
我怎样才能完成这项工作?任何帮助将不胜感激。
【问题讨论】:
标签: ruby-on-rails-3 sprockets tilt