title: Rails 4: escape_javascript render partial not working
body: <p>In my Rails 4 app, I am trying to display some views as <code>modals</code> (with Bootstrap modal), instead of the regular <code>.html.erb</code> views.</p>
  
  <p>For instance, I have a <code>calendar</code> model, with a custom <code>analysis</code> action in the controller:</p>
  
  <pre><code>def analysis
    @calendar = Calendar.find(params[:id])
    respond_to do |format|
      format.js
    end
  end
  </code></pre>
  
  <p>Following what is explained in <a href="http://www.benkirane.ch/ajax-bootstrap-modals-rails/" rel="nofollow noreferrer">this tutorial</a> and <a href="https://coderwall.com/p/ej0mhg/open-a-rails-form-with-twitter-bootstrap-modals" rel="nofollow noreferrer">that other tutorial</a>, I am trying to create the following files:</p>
  
  <pre><code># _dialog.html.erb
  
  &lt;div class="modal fade" id="dialog" tabindex="-1" role="dialog"  aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="true"&gt;
    &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" 
                  data-dismiss="modal" 
                  aria-label="Close"&gt;
                  &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
          &lt;h3 class="modal-title"&gt;&lt;/h3&gt;
        &lt;/div&gt;
        &lt;div class="modal-body"&gt;
        &lt;/div&gt;
  
      &lt;/div&gt;&lt;!-- /.modal-content --&gt;
    &lt;/div&gt;&lt;!-- /.modal-dialog --&gt;
  &lt;/div&gt;&lt;!-- /.modal --&gt;
  </code></pre>
  
  <hr>
  
  <pre><code># _analysis.html.erb
  
  &lt;p&gt;&lt;%= @calendar.name %&gt; Analysis&lt;/p&gt;
  </code></pre>
  
  <hr>
  
  <pre><code># analysis.js.erb
  
  // Add the dialog title
  $('#dialog h3').html("Calendar Analysis");
  
  // Render calendar analysis
  $('.modal-body').html('&lt;%= j render(:partial =&gt; 'calendars/analysis') %&gt;');
  
  // Show the dynamic dialog
  $('#dialog').modal("show");
  
  // Set focus to the first element
  $('#dialog').on('shown.bs.modal', function () {
      $('.first_input').focus()
    })
  </code></pre>
  
  <p>Last but not least, I call the modal from a <code>_heading.html.erb</code> partial, rendered inside the calendar <code>show.html.erb</code> view, with the <code>&lt;%= render 'analysis' %&gt;</code> helper and the following link:</p>
  
  <pre><code>&lt;%= link_to '&lt;i class="glyphicon glyphicon-dashboard"&gt;&lt;/i&gt;'.html_safe, calendar_analysis_path, :remote =&gt; true, 'data-toggle' =&gt;  "modal", 'data-target' =&gt; '#modal-window' %&gt;
  </code></pre>
  
  <p>When I launch my app, I then get the following error:</p>
  
  <pre><code>undefined method `render' for #&lt;#&lt;Class:0x007fee248d8118&gt;:0x007fee23577ce0&gt;
  </code></pre>
  
  <p>—————</p>
  
  <p><strong>UPDATE</strong>: I have tried different ways to render the partial:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11127934/rendering-partial-in-js-erb-file">As recommended here</a>, I tried both <code>escape_javascript render</code> and <code>j render</code>.</li>
  <li><a href="https://stackoverflow.com/questions/25313911/rails-js-erb-not-rendering-partial">As recommended there</a>, I have also tried <code>render_to_string</code> instead of just <code>render</code>.</li>
  <li>As recommended in that other question, I even tried different ways to call the partial, both with <code>render(:partial =&gt; 'calendars/analysis')</code> and <code>(render 'calendars/analysis')</code>.</li>
  </ul>
  
  <p>But none of these solutions worked.</p>
  
  <p>—————</p>
  
  <p>I have done quite a lot of research on that topic and I have to say that I am confused now.</p>
  
  <p>While both tutorials mentioned above recommend this approach, other sources, <a href="https://stackoverflow.com/questions/8370399/rails-js-erb-file-cannot-find-method-render">including this one</a>, point out that you cannot render a partial from assets in Rails.</p>
  
  <p>Therefore:</p>
  
  <ol>
  <li>What is wrong with my current code?</li>
  <li>Does the approach I am trying to use make sense at all, and if not, what shoud I do instead?</li>
  </ol>