我正在实施类似netflix的5星评价。目标是拥有一个“评级”div,通过点击其中一个星号来输入评级,并显示当前评级。我想在用户输入新评级后更新“评级”。
目前,我可以添加评级并创建与评级资产的关联,但在保存数据后无法刷新“评级”div ...我缺少什么?
评级控制器:
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
评分部分(评级/ _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
来自控制台的输出:
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]
以下是“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>
答案 0 :(得分:2)
找出如何解决它:我将“更新”和“部分”参数从评级部分移动到评级&gt;创建控制器......现在看来是这样的:
评级控制器:我更改了渲染选项
render :update do |page|
page.replace_html 'star-ratings-block', :partial => 'ratings/rating', :locals => { :asset => @course }
end
部分“rating / _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>
部分包含在主视图中:
<div 'star-ratings-block'>
<%= render "ratings/rating", :locals => { :asset => yourvariablehere } %>
</div>
它现在可以正常工作!