title: Render remote partial via controller on load
body: <p><strong>How do I render a partial view via its controller when loading a separate view in Ruby on Rails?</strong></p>
  
  <p><strong>Thanks for your help in advance</strong></p>
  
  <p><strong>Problem</strong>: I have a <code>show.html.erb</code> for my <code>Patients</code> controller, when this loads I have a series of tabs, one of which is a list of notes added to that patient record. <code>Notes</code> is a separate model. I want to load the list of notes when loading <code>show.html.erb</code>.</p>
  
  <p>I can use the code below to render the view directly and can pass variables in this way, but I would rather go through the controller and deliver <code>notes/_list.js.erb</code>, via the <code>Patients Controller</code>.</p>
  
  <pre><code>&lt;%= render "notes/list", :remote =&gt; true %&gt;
  </code></pre>
  
  <p>I have a solution that loads the partial via a <code>link_to</code> but because the link is in a tab it then breaks my tab javascript, and would rather just load the information on initial page load rather than have the user interact with an element.</p>
  
  <pre><code>&lt;%= link_to "Notes".html_safe,{:controller =&gt; "patients", :action =&gt; "notes", :remote =&gt; true } %&gt;
  </code></pre>
  
  <p>It needs to be in a partial as I am then using <code>pagy</code> to paginate the results and if I display it via a controller dependent partial then the pagy navigation links function as expected.</p>
  
  <hr>
  
  <p><strong>Relevant Routes</strong></p>
  
  <pre><code>get 'patients/:id/show' =&gt; 'patients#show', as: :show_patient
  get 'patients/:id/notes' =&gt; 'patients#notes', as: :notes_patient
  </code></pre>
  
  <p><strong>Relevant Patients Controller</strong></p>
  
  <pre><code>def notes
  
      @pagy, @notes = pagy( Note.where(:patient_id =&gt; params[:id]).reverse_order, items: 2 )
  
      respond_to do |format|
          format.html { render "notes/list" }
          format.js { render "notes/list"  }
      end
  
  
  end
  </code></pre>
  
  <p><strong>notes/list.js.erb</strong></p>
  
  <pre><code>$('#notes-list').html('&lt;%= escape_javascript(render('notes/list')) %&gt;');
  </code></pre>
  
  <p><strong>notes/_list.html.erb</strong></p>
  
  <pre><code>    &lt;% @notes.each do |note| %&gt;
          &lt;%= render partial: note %&gt;
      &lt;% end %&gt;
  
      &lt;%== pagy_materialize_nav(@pagy) %&gt;
      &lt;span class="pagy-info"&gt;&lt;%== pagy_info(@pagy) %&gt;&lt;/span&gt;
  </code></pre>