在 Rails 中重用 js.rjs 文件?
我有一个 .js.rjs 文件,必须在很多地方使用它。
我怎样才能重复使用它?
例如,在我的 .js.rjs 文件中,我需要这样的内容:
page << ( render "widely_used_stuff" )
其中“widely_used_stuff”是一个widely_used_stuff.js.rjs 文件,其中包含在许多地方使用的代码。
另外,我需要将变量传递给widely_used_stuff.js.rjs,就像这样:
page << ( render "widely_used_stuff", :locals => {:custom_script => my_script} )
更新1
我已经尝试了.js.rjs文件中的以下方法:
render "widely_used_stuff"
Rails抱怨它,因为它要求这个“部分”为“erb”部分
以下方式:
render :file => "controller/widely_used_stuff.js.rjs"
Rails 不会抱怨,但“widely_used_stuff.js.rjs”不会插入到 .js.rjs 文件中,该文件称为渲染。我已经检查了 XHR 响应。
我认为,问题出在调用者 .js.rjs 文件中,因为它不是 erb...
i have an .js.rjs file, that must be used in many places.
How can i reuse it ?
For example, in my .js.rjs file i need something like this:
page << ( render "widely_used_stuff" )
where "widely_used_stuff" is a widely_used_stuff.js.rjs file, that contains the code, used in many places.
Also, i need to pass the variables to widely_used_stuff.js.rjs, like in this way:
page << ( render "widely_used_stuff", :locals => {:custom_script => my_script} )
Update 1
I have tried following ways from .js.rjs file:
render "widely_used_stuff"
Rails complains on it, because it requires this "partial" to be "erb" partial
Following way:
render :file => "controller/widely_used_stuff.js.rjs"
Rails don't complains, but "widely_used_stuff.js.rjs" is not inserted to .js.rjs file, that called render. I have checked the XHR response.
I think, the problem is in the caller .js.rjs file, because it is not a erb...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您好,您尝试过
渲染“controller/widely_used_stuff.js.rjs”
吗?Hi have you tried
render "controller/widely_used_stuff.js.rjs"
?我希望能有所帮助。
i hope could help .