title: Rails redirect user back to RESTful partial generated with AJAX
body: <p>I'm not sure if the title makes sense, but it does have all the components of what is going on... let me explain.</p>
  
  <p>I have a view <code>/settings</code> which looks like below:</p>
  
  <p><a href="https://i.stack.imgur.com/LDV9D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LDV9D.png" alt="/settings page"></a></p>
  
  <p>The code for the view is:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%= render partial: "pages/navigation" %&gt;
  
    &lt;div class="row"&gt;
        &lt;!-- sidebar --&gt;
        &lt;div class="col-md-2 sidebar"&gt;
          &lt;h3&gt;Your Devices:&lt;/h3&gt;
            &lt;ul class="device_list"&gt;
              &lt;% @devices.each do |device| %&gt;
                &lt;li class="device"&gt;
  
                  &lt;% if device.last_checkin &amp;&amp; device.last_checkin &gt; 30.minutes.ago %&gt;
                    &lt;a class="btn btn-success btn-circle-micro"&gt;&lt;/a&gt;
                  &lt;% else %&gt;
                    &lt;a class="btn btn-danger btn-circle-micro"&gt;&lt;/a&gt;
                  &lt;% end %&gt;
  
                  &lt;%= link_to device.name, edit_scan_option_path(id: device.id), :remote =&gt; true %&gt;
                &lt;/li&gt;
              &lt;% end %&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
  
        &lt;!-- content window --&gt;
        &lt;div class="col-md-10 content"&gt;
          &lt;%= render partial: 'view_window' %&gt;
        &lt;/div&gt;
    &lt;/div&gt;
  
  &lt;%= render partial: "pages/footer" %&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here is the edit method in the <code>scan_options controller</code>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>def edit
      @selected = ScanOption.where(device_id: params[:id])
      @device = Device.find(params[:id])
  
      @device.scan_options.new unless @device.scan_options.present?
  
      respond_to do |format|
        format.js
      end
    end</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>/settings/_view_window.html.erb partial</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="settings_window"&gt;
    &lt;%= render partial: "scan_options/device_settings", locals: { device: @device } %&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>and then <strong>scan_options/edit.js.erb</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>$("#settings_window").html("&lt;%= escape_javascript(render partial: 'device_settings', locals: { device: @selected } ) %&gt;");</code></pre>
  </div>
  </div>
  </p>
  
  <p>Which loads <strong>/scan_options/_device_settings.html.erb</strong> which loads the partial to the right in the screenshot above.  At this point, the url in the browser is still <code>/settings</code> however the user is actually on <code>/settings/11/edit</code> behind the scene.</p>
  
  <p>The problem is that after the user clicks save they are redirected back to <code>/settings</code> which is confusing to them since it will always load the first one again, in this case the partial for the Device on the left (Aurora), whereas they might have been editing the Device (Test JSON6) or something else.  </p>
  
  <p>So, how can I redirect the user back to <code>/settings/11/edit</code>, even though the url will read `/settings'?</p>
  
  <p>I'm not sure if I've included everything that is needed, so please let me know if I need to add any additional code.</p>