title: Javascript as response to controller action displays it's own code
body: <p>This is bugging me for already three days. I have a form which I submit and after that I inject javascript code into page depending about success of validation. My controller action is simple and it looks like this:</p>
  
  <pre><code>def create
      @cat = Cat.new(cat_params)
      @cat.save
  end
  </code></pre>
  
  <p>Which triggers <code>create.js.erb</code>:</p>
  
  <pre><code>&lt;% if @cat.new_record? %&gt;
      $('#my_div').html('&lt;%= j render('form') %&gt;');
  &lt;% else %&gt;
      $('#cats').append("done");
  &lt;% end %&gt;
  </code></pre>
  
  <p>First line in <code>create.js.erb</code> checks if the cat is new record. If it is, it means validation failed and it renders a new form with validation errors. This is where I have a problem. The new form shows together with errors but when I try to submit a form again, it doesn't work and it just displays code of form on another page. So, imagine a completely blank page on which is displayed this code from <code>create.js.erb</code> in case of success:</p>
  
  <pre><code>$('#my_div').html('ALL CODE OF MY RE-RENDERED FORM. Really all source code of my form is displayed here with all tags and everything. Like javascript didn't insert anything.');
  </code></pre>
  
  <p>And in case of failure:</p>
  
  <pre><code>$('#cats').append("done");    
  </code></pre>
  
  <p>Javascript code is not active at all. I have a remote form which is used for creation of new <code>cat</code> defined like this:</p>
  
  <pre><code>&lt;%= form_for @cat, remote: true, format: 'js', method: 'post', :html =&gt; {:multipart =&gt; true} do |f| %&gt;
  </code></pre>
  
  <p>I am using jQuery-File-Upload for my form but it shouldn't be a problem in this case:</p>
  
  <pre><code>jQuery -&gt;
    $('#new_cat').fileupload
      dataType: "script"
      add: (e, data) -&gt;
        types = /(\.|\/)(gif|jpe?g|png)$/i
        file = data.files[0]
        if types.test(file.type) || types.test(file.name)
          data.context = $(tmpl("template-upload", file))
          $('#new_cat').append(data.context)
        else
          alert("#{file.name} is not a gif, jpeg, or png image file")
  
        $("#submit_button").on 'click', -&gt;
          data.submit()
  
      progress: (e, data) -&gt;
        if data.context
          progress = parseInt(data.loaded / data.total * 100, 10)
          data.context.find('.bar').css('width', progress + '%')
  </code></pre>
  
  <p>I really don't get it what could be a problem here. I am using turbolinks so maybe that could be a problem but I don't understand why javascript which is a response to certain action would not be translated. Any ideas what could be a problem?</p>