Ruby on Rails View inserting js.erb file via render

Yo Ludke picture Yo Ludke · Jan 10, 2013 · Viewed 9k times · Source

For including a piece of javascript that I only need on some pages, I put

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

in my haml file. 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. How should I input a JS-ERB correctly?

UPDATE:

The js.erb file has the form

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

Answer

Yo Ludke picture Yo Ludke · Jan 10, 2013

Nice :) I think I found the answer on my own with this SO article

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

Works, and doesn't give me the Deprecation warning