Rails如何创建ajax星级评级

时间:2011-04-02 22:29:52

标签: jquery ruby-on-rails ruby ruby-on-rails-3

我正在尝试使用rate it jquery创建一些简单的ajax评分。 我能够看到星星并按下它们。但是我的表格没有提交。

如何使用ajax提交评级?

在我的表中,我有两列:

rating_score => The current score
ratings => The number of ratings which led to the score

我的分类显示视图:

<h1><%= @kategori.h2.force_encoding("UTF-8") %></h1>
<div id="konkurrancer"><%= render 'konkurrencer', :remote => true %></div>

我的发生者部分:

<div id="tabel">
  <table id="tabel1">
    <tr id="toptr">
      <td><%= sortable "name", "Navn" %></td>
    </tr>
    <% @konkurrancer.each do |vind| %>
      <tr class="thumbnail-item" onclick="window.open('<%= vind.tracking %>')">
        <td><%= truncate(vind.name.force_encoding("UTF-8"), :length => 45) %></td>
        <td>
          <div id="container">
            <%= render :partial => "rating", :remote => true, :locals => { :vind => vind } %>
          </div>
        </td>
      </tr>
    <% end %>
  </table>
  <div id="pagenavi">
    <%= hidden_field_tag :direction, params[:direction] %>
    <%= hidden_field_tag :sort, params[:sort] %>
  </div>
</div>

我的评分偏:

<%= form_for(:vind, :url => rate_path(vind.id), :remote => true, :html => { :method => 'put', :id => "rateform"}) do |f| %>
<%= f.text_field :ratings, :size => 1, :min => 1, :max => 6, :value => 1, :type => 'range', :step => '1', :id => "#{vind.id}" %>
<div class="rateit" id="rateit10b" data-rateit-backingfld="#<%= vind.id %>" data-rateit-resetable="false" ></div>
<% end %>
  <script type ="text/javascript">
     $('.rateit').bind('rated reset', function (e) {
         var ri = $(this);

         //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
         var value = ri.rateit('value');
         var frmurl = ri.closest('form').attr('action'); //get the url from the form

         $.ajax({
             url: frmurl, //your server side script
             data: { ratings: value }, //our data
             type: 'PUT',
             success: function (data) {
                  //write here something when everything went ok

             },
             error: function (jxhr, msg, err) {
                 alert(msg); //something went wrong.
             }
         });
     });
 </script>

我的rate.js:

$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { konkurrancer: @konkurrancer })) %>');

我的控制器:

class KonkurrancersController < ApplicationController
  def rate
    @konkurrancer = Konkurrancer.find(params[:id])
    @container = "Konkurrancer"+@Konkurrancer.id.to_s

    @theme.rating_score += params[:rating].to_i
    @theme.ratings += 1
    @theme.save

    respond_to do |format|
      format.js
    end
  end
end

这是我在浏览器中的评分。当我按下gem Vind时,params评级是用值为5的ajax发送的。问题是我希望它在按下星星时发送而不是使用提交按钮。 enter image description here

提交表单时的我的控制台:

Started POST "/konkurrancers/rate/12" for 127.0.0.1 at 2011-04-04 23:37:56 +0200

  Processing by KonkurrancersController#rate as JS
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"3IqZ6IfvaFYQnVeMYu2a09PcCnm
VW52vaEp2KjfJ1g8=", "vind"=>{"rating"=>"6"}, "commit"=>"Gem Vind", "id"=>"12"}
  ←[1m←[35mKonkurrancer Load (1.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurr
ancers`
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `konkurrancers`←
[0m
  ←[1m←[35mCACHE (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurrancers`
  ←[1m←[36mKonkurrancer Load (1.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers` WHERE (`konkurrancers`.`cached_slug` = '12') LIMIT 1←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  SELECT sluggable_id FROM slugs WHERE ((slugs.sluggab
le_type = 'Konkurrancer' AND slugs.name = '12' AND slugs.sequence = 1))
  ←[1m←[36mKonkurrancer Load (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers` WHERE (`konkurrancers`.`id` = 12) LIMIT 1←[0m
  ←[1m←[35mSQL (0.0ms)←[0m  BEGIN
  ←[1m←[36mSlug Load (22.0ms)←[0m  ←[1mSELECT `slugs`.* FROM `slugs` WHERE (`slu
gs`.sluggable_id = 12 AND `slugs`.sluggable_type = 'Konkurrancer') ORDER BY id D
ESC LIMIT 1←[0m
  ←[1m←[35mAREL (14.0ms)←[0m  UPDATE `konkurrancers` SET `rating_score` = 17, `r
atings` = 5, `updated_at` = '2011-04-04 21:37:57' WHERE (`konkurrancers`.`id` =
12)
  ←[1m←[36mSQL (16.0ms)←[0m  ←[1mCOMMIT←[0m
Rendered konkurrancers/_rating.html.erb (1.0ms)
Rendered konkurrancers/rate.js.erb (24.0ms)
Completed 200 OK in 439ms (Views: 226.0ms | ActiveRecord: 55.0ms)

我的html视图:http://pastie.org/1756195

1 个答案:

答案 0 :(得分:5)

我是RateIt插件的作者。 我对Ruby On Rails有一个小小的线索,但我会尝试尽可能好地回答这个问题。

评估某些内容时,RateIt控件不会自动提交。您必须绑定到rated事件。

如果您的表单设置正确,则以下Javascript应该有效:

AJAX提交:

<script type ="text/javascript">
     jQuery('.rateit').bind('rated reset', function (e) {
         var ri = jQuery(this);

         //disable voting...
         ri.rateit('readonly', true); 
         //add a span with the selected rating after the rateit plugin.
         ri.after('<span class="rating">' + ri.rateit('value') + '</span>');

         //if the use pressed reset, it will get value: 0 (to be compatible with the HTML range control), we could check if e.type == 'reset', and then set the value to  null .
         var frm = ri.closest('form');

         jQuery.ajax({
             url: frm.attr('action'), //your server side script
             data: frm.serialize(), //our data
             type: 'POST',
             success: function (data) {
                  //write here something when everything went ok

             },
             error: function (jxhr, msg, err) {
                 alert(msg); //something went wrong.
             }
         });
     });
 </script>

非AJAX提交

 <script type ="text/javascript">
     jQuery('.rateit').bind('rated reset', function (e) {
         jQuery(this).closest('form').submit();
     });
 </script>