【发布时间】:2014-02-24 20:27:16
【问题描述】:
在我的控制器中,我将某个视图存储在一个变量 @template 中,设置一个测试变量并期望一些 js 响应
@template = ActionView::Base.new('app/views/devise/sessions', {}, ActionController::Base.new).render(file: 'new').to_s
@test="test"
respond_to do |format|
format.js {render 'layouts/some_file.js.erb'}
end
然后我将此变量传递给some_file.js.erb 并调用:
$("body").append('<%= escape_javascript(@template) %>');
假设我在@tempate 中输入了,那么@test 为空。为什么会这样?
【问题讨论】:
-
因为您的上下文不提供对@test 变量的访问
-
啊,我明白了,有没有办法做到这一点?
-
阅读文档:apidock.com/rails/ActionView/Base/new/class 第二个参数是赋值,听起来应该是个提示
-
非常感谢,它成功了!你能把它作为答案发布吗
标签: javascript ruby-on-rails ruby