title: Updating a partial via Ajax with Rails
body: <p>I am trying to update a rails partial while using Ajax and having some trouble.  Basically I have a Post with a Karma attribute.  Users can vote up and vote down to take and remove Karma from the post.  I would like to allow the user to asynchronously update the post value using Ajax and im running into a NilClass error that im hoping you can help me with.
  The error im getting is undefined method `karma' for nil:NilClass (in my _karma_value.erb file), and my files look like below.  Im totally stuck!</p>
  
  <p>posts_controller.rb</p>
  
  <pre><code>   def add_karma
       # Save the page that the request came from
       session[:initial_page] ||= request.referer
  
       @post = Post.find(params[:id])
       @post.karma = @post.karma + 1
       @post.update_attributes(params[:post])
  
       respond_to do |format|
         format.html { redirect_to session[:initial_page] }  #redirect back to requesting page
         format.json { head :no_content }
         format.js {}
       end   
     end
  </code></pre>
  
  <p>add_karma.js.erb</p>
  
  <pre><code>   $('#add_karma').html(
       "&lt;%= escape_javascript(render('_karma_value', post: @post)) %&gt;");
  </code></pre>
  
  <p>index.html.erb</p>
  
  <pre><code>...
  &lt;b&gt;Karma:&lt;/b&gt; 
          &lt;%= render(:partial =&gt; 'karma_value', :locals =&gt; {:post =&gt; @post}) %&gt;  &lt;br&gt;
  ...
  </code></pre>
  
  <p>_karma_value.erb</p>
  
  <pre><code>&lt;% if !(post.karma.nil?) %&gt;
      &lt;%= post.karma %&gt;
  &lt;% end %&gt;
  </code></pre>