简体   繁体   English

Ruby on Rails查看通过渲染插入js.erb文件

[英]Ruby on Rails View inserting js.erb file via render

For including a piece of javascript that I only need on some pages, I put 为了包含我在某些页面上只需要的一段javascript,我放了

 = javascript_tag do
   = render "inserts_js_enhanced_element.js.erb"

in my haml file. 在我的haml文件中。 Now I get the warning in my tests: 现在我在测试中收到警告:

DEPRECATION WARNING: Passing a template handler in the template name is 
deprecated. You  can simply remove the handler name or pass render
:handlers => [:erb] instead.`

However, when I change the line to 但是,当我将线路更改为

   = render "inserts_js_enhanced_element.js", :handlers => [:erb]

I get a new warning anyway, saying 无论如何,我得到一个新的警告,说

 DEPRECATION WARNING: Passing the format in the template name is 
 deprecated. Please pass render with :formats => [:js] instead.

If I do change this to 如果我确实改变了

 = render "inserts_js_enhanced_element", :handlers => [:erb], :formats => [:js]

it gives me the error 它给了我错误

     ActionView::Template::Error:
   Missing partial application/inserts_js_enhanced_element with {:handlers=>[:haml, :builder, :erb, :coffee], :locale=>[:de], :formats=>[:html]}. 

Now the formats doesn't seem to have changed to :js and also the handlers are not only :erb . 现在格式似乎没有改为:js和处理程序不仅仅是:erb How should I input a JS-ERB correctly? 我应该如何正确输入JS-ERB?

UPDATE: 更新:

The js.erb file has the form js.erb文件具有表单

 <% session_val = determine_session_val_from_config %>
 jQuery(document).ready(function() {
   //JS Code
 }

Nice :) I think I found the answer on my own with this SO article 很好:)我想我自己找到了这篇 SO文章的答案

= render(:partial => 'inserts_js_enhanced_element', :handlers => [:erb], :formats => [:js])

Works, and doesn't give me the Deprecation warning 工作,并没有给我折旧警告

你尝试过这个吗?

= render "inserts_js_enhanced_element"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM