title: Ruby on Rails, update div on click with link_to_remote
body: <p>I'm implementing a netflix-like 5 star review ratings. The goal is to have a "rating" div that serves both to enter the rating by clicking on one of the stars, as well to display the current rating. I want to update the "rating" after a users enters a new rating.</p>
  
  <p>For now, I can add a rating and create the association to the rated asset, but can't refresh the "rating" div after the data is saved... What am I missing?</p>
  
  <p><strong>Ratings Controller:</strong></p>
  
  <pre><code>  def create
      case params[:rating][:rateable_type]
      when "course"
        @course = Course.find(params[:rating][:rateable_id])
      end
  
      @rating = Rating.new(params[:rating])
  
      if @rating.save
        @course.ratings_sum += @rating.rating
        @course.save
        flash[:notice] = 'Rating was successfully created.'
        render :partial =&gt; "ratings/rating", :locals =&gt; { :asset =&gt; @course }
      end
    end
  </code></pre>
  
  <p><strong>Ratings partial (ratings/_rating.html.erb):</strong></p>
  
  <pre><code>&lt;div id="rating"&gt;
  &lt;%= number_with_precision(asset.avg_rating, :precision =&gt; 1) %&gt;/5 Stars&lt;br&gt;
  
  &lt;ul class='star-rating'&gt;
      &lt;li class='current-rating' style='width:&lt;%= (asset.avg_rating * 17).to_s -%&gt;px;'&gt;
          Currently &lt;%= number_with_precision(asset.avg_rating, :precision =&gt; 1) %&gt;/5 Stars.
      &lt;/li&gt;
      &lt;li&gt;
          &lt;%= link_to_remote "1", :url =&gt; {:controller =&gt; "ratings", :action =&gt; :create},
              :update =&gt; 'rating', :partial =&gt; "ratings/rating",
              :rating =&gt; {:rateable_type =&gt; asset.class.to_s.downcase, :rateable_id =&gt; asset.id, :rating =&gt; 1},
              :method =&gt; :post, :class =&gt; 'one-star', :name =&gt; '1 star out of 5' %&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;%= link_to_remote "2", ...
  &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Routes.rb:</strong></p>
  
  <p><code>
    map.resources :ratings</p>
  
  <p></code></p>
  
  <p><strong>Output from the console:</strong></p>
  
  <pre><code>Processing RatingsController#create (for 127.0.0.1 at 2010-10-13 15:09:14) [POST]
    Parameters: {"rating"=&gt;{"rating"=&gt;"1", "rateable_type"=&gt;"course", "rateable_id"=&gt;"42"}, "authenticity_token"=&gt;"IodWLi9JO56tco6rgQH5vtdvKTNsE/Fih0k9jWptZmk=", "update"=&gt;"rating"}
    Course Columns (1.8ms)   SHOW FIELDS FROM `courses`
    Course Load (0.1ms)   SELECT * FROM `courses` WHERE (`courses`.`id` = 42) 
    Rating Columns (0.9ms)   SHOW FIELDS FROM `ratings`
    SQL (0.1ms)   BEGIN
    Rating Create (0.2ms)   INSERT INTO `ratings` (`rating`, `rateable_type`, `created_at`, `rateable_id`, `updated_at`, `user_id`) VALUES(1, 'course', '2010-10-13 22:09:14', 42, '2010-10-13 22:09:14', 0)
    Course Load (0.3ms)   SELECT * FROM `courses` WHERE (`courses`.`id` = 42) 
    Course Update (0.3ms)   UPDATE `courses` SET `ratings_count` = COALESCE(`ratings_count`, 0) + 1 WHERE (`id` = 42) 
    SQL (0.4ms)   SHOW TABLES
    Course Update (0.3ms)   UPDATE `courses` SET `updated_at` = '2010-10-13 22:09:14', `ratings_sum` = 20 WHERE `id` = 42
    SQL (0.8ms)   COMMIT
    SQL (0.1ms)   BEGIN
    Course Update (0.2ms)   UPDATE `courses` SET `updated_at` = '2010-10-13 22:09:14', `ratings_sum` = 20 WHERE `id` = 42
    SQL (0.1ms)   COMMIT
  Rendered ratings/_rating (9.7ms)
  Completed in 43ms (View: 11, DB: 6) | 200 OK [http://localhost/ratings?rating%5Brateable_id%5D=42&amp;rating%5Brateable_type%5D=course&amp;rating%5Brating%5D=1&amp;update=rating]
  </code></pre>
  
  <p><strong>And here is the code generated by "link_to_remote"</strong></p>
  
  <pre><code>&lt;a onclick="new Ajax.Request('/ratings?rating%5Brateable_id%5D=42&amp;amp;rating%5Brateable_type%5D=course&amp;amp;rating%5Brating%5D=1&amp;amp;update=rating_bar', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('IodWLi9JO56tco6rgQH5vtdvKTNsE/Fih0k9jWptZmk=')}); return false;" name="1 star out of 5" method="post" href="#" class="one-star"&gt;1&lt;/a&gt;
  </code></pre>