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.
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?
Ratings Controller:
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 => "ratings/rating", :locals => { :asset => @course }
end
end
Ratings partial (ratings/_rating.html.erb):
<div id="rating">
<%= number_with_precision(asset.avg_rating, :precision => 1) %>/5 Stars<br>
<ul class='star-rating'>
<li class='current-rating' style='width:<%= (asset.avg_rating * 17).to_s -%>px;'>
Currently <%= number_with_precision(asset.avg_rating, :precision => 1) %>/5 Stars.
</li>
<li>
<%= link_to_remote "1", :url => {:controller => "ratings", :action => :create},
:update => 'rating', :partial => "ratings/rating",
:rating => {:rateable_type => asset.class.to_s.downcase, :rateable_id => asset.id, :rating => 1},
:method => :post, :class => 'one-star', :name => '1 star out of 5' %>
</li>
<li>
<%= link_to_remote "2", ...
</ul>
</div>
Routes.rb:
map.resources :ratings
Output from the console:
Processing RatingsController#create (for 127.0.0.1 at 2010-10-13 15:09:14) [POST]
Parameters: {"rating"=>{"rating"=>"1", "rateable_type"=>"course", "rateable_id"=>"42"}, "authenticity_token"=>"IodWLi9JO56tco6rgQH5vtdvKTNsE/Fih0k9jWptZmk=", "update"=>"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&rating%5Brateable_type%5D=course&rating%5Brating%5D=1&update=rating]
And here is the code generated by "link_to_remote"
<a onclick="new Ajax.Request('/ratings?rating%5Brateable_id%5D=42&rating%5Brateable_type%5D=course&rating%5Brating%5D=1&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">1</a>
Found out how to solve it: I moved the "update" and "partial" parameters from the ratings partial to the Ratings > Create controller... Here is what it looks like now:
Ratings Controller: I changed the render options
render :update do |page|
page.replace_html 'star-ratings-block', :partial => 'ratings/rating', :locals => { :asset => @course }
end
Partial "ratings/_rating.html.erb":
<div id="rating_div">
<%= number_with_precision(asset.avg_rating, :precision => 1) %>/5 Stars<br>
<ul>
<li>
<%= link_to_remote "1", {:url => { :controller => "ratings", :action => :create,
:rating => {:rateable_type => asset.class.to_s.downcase, :rateable_id => asset.id, :rating => 1}}},
:method => :post, :class => 'one-star', :name => '1 star out of 5' %>
</li>
...
</ul>
</div>
The partial is included in the main view like that:
<div 'star-ratings-block'>
<%= render "ratings/rating", :locals => { :asset => yourvariablehere } %>
</div>
It now works as desired!