title: Render partial with refreshed data via AJAX
body: <p>I am pretty new to JS, but I hope I am missing something simple. I am trying to reload a partial that displays an image asynchronously. When a user clicks on the link below, it registers their vote, then (it should) render the pins partial with a new image from the database. </p>
  
  <p><strong>index.html.erb (The link)</strong></p>
  
  <pre><code>&lt;%= link_to 'Less', {:controller =&gt; "pins", 
                       :action =&gt; "create_view", 
                       :rank =&gt; 0,
                       :user_id =&gt; current_user.id, 
                       :pin_id =&gt; @pins.id
                       },
                       :method =&gt; "post", :remote =&gt; true %&gt;
  </code></pre>
  
  <p><strong>pins_controller.rb (Method called by link)</strong></p>
  
  <pre><code>def create_view
    @view = View.new
    @view.user_id = params[:user_id]
    @view.pin_id = params[:pin_id]
    @view.rank = params[:rank]
  
    if @view.save
     respond_to do |format|
      format.html { redirect_to root_path }
      format.js
     end
    else
     render action: "new"
    end
  end
  </code></pre>
  
  <p><strong>pins_controller.rb (Method that generates @pins)</strong></p>
  
  <pre><code>def index
    seen = Array.new
    max_pins = 20
    sex = current_user.sex
    views = View.user_views(current_user)
    seen = views.map(&amp;:pin_id)
    @pins = Pin.new_pin(seen, sex)
  </code></pre>
  
  <p><strong>models/pins.rb</strong> </p>
  
  <pre><code>def self.new_pin(pin_ids, sex)
    b = TRUE
    self.find(:first, :conditions =&gt; ["sex = ? AND active = ? AND id not in (?)", sex, b,    pin_ids], :order =&gt; "created_at desc" )
  end
  </code></pre>
  
  <p><strong>index.html.erb (The DIV and partial)</strong></p>
  
  <pre><code>&lt;div id="main_well"&gt;
       &lt;%= render @pins %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>create_views.js.erb (The javascript triggered on click)</strong> </p>
  
  <pre><code>$('#main_well').html("&lt;%= escape_javascript(render (partial: "pin", :locals =&gt; { pin: @pins})) %&gt;");
  </code></pre>
  
  <p><strong>_pin.html.erb (The partial to be reloaded by JS)</strong></p>
  
  <pre><code>&lt;div class="row"&gt;
   &lt;div class="center_well"&gt;
     &lt;div class="well" style="background-image: url('&lt;%= pin.image %&gt;')" onclick="window.open('&lt;%= pin.product_url %&gt;','mywindow');"&gt;&lt;/div&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have been at this for a while. Here's what I know. Clicking the link does remotely update the database. And if I change the JS to $('#main_well').remove(), clicking the link does remove the image asynchronously. However, I cannot for the life of my get the image to update onclick without refreshing the page. Thanks in advance for your help.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As in the answer below, the instance variable @pins was not leading in the create_views method. Thus, regenerating the instance variable link this seemed to solve the problem.</p>
  
  <p><strong>pins_controller.rb</strong></p>
  
  <pre><code>def create
   seen = Array.new
   max_pins = 20
   sex = current_user.sex
   views = View.user_views(current_user)
   seen = views.map(&amp;:pin_id)
   @pins = Pin.new_pin(seen, sex)
  
   @view = View.new
   @view.user_id = params[:user_id]
   @view.pin_id = params[:pin_id]
   @view.rank = params[:rank]
  
   if @view.save
    respond_to do |format|
       format.html { redirect_to root_path }
       format.js
    end
   else
    render action: "new"
   end
  end
  </code></pre>
  
  <p>However, this solution only serves to reload the partial successfully once. If I click the link, it reloads the image. However, if I click the link again, nothing happens. However, if I change the function like so it works perfectly:</p>
  
  <p><strong>pins_controller.rb</strong></p>
  
  <pre><code>def create
   seen = Array.new
   max_pins = 20
   offset = rand(Pin.count)
   @pins = Pin.first(:offset =&gt; offset)
  
   @view = View.new
   @view.user_id = params[:user_id]
   @view.pin_id = params[:pin_id]
   @view.rank = params[:rank]
  
   if @view.save
    respond_to do |format|
       format.html { redirect_to root_path }
       format.js
    end
   else
    render action: "new"
   end
  end
  </code></pre>
  
  <p>It seems that the local variable "view" is not being reloaded the second time I click the link. I have no idea why. Any help would be greatly appreciated.</p>
  
  <p><strong>views.rb</strong></p>
  
  <pre><code>def self.user_views(user)
    self.find(:all, :conditions =&gt; ['user_id = ?', user])
  end
  </code></pre>