Rails 5星级jQuery插件Raty没有通过评分

时间:2019-02-04 15:09:21

标签: jquery ruby-on-rails rating raty

我是Rails的新手,并且我正在使用Star Rating jQuery Plugin Raty进行评论/评分。现在,我有一个需要检查的数据列表,然后,检查方法将在boostrap模式框中,如下图所示。

enter image description here

一切正常,但是当我单击图片中的提交按钮(显示保存更改)时,我没有收到评分参数,收到的参数如下图所示。

enter image description here

预期结果也将包括评分值。我已按照以下步骤在application.js中添加jquery.raty,并在vendor / assets / javascripts中分配了文件。另外,我还在文件中添加了此javascript代码。

<script>
$('#star-rating').raty({
    path: '/assets',
    scoreName: 'review[rating]'
});
</script>

一切正常,但没有通过评级的值。我检查了结果,发现该值实际上保存在jQuery插件中包含的hidden_​​tag中,但是在提交表单时并没有通过。

这是我的表格和模式框代码

<% @appointment.approve_applicants.each_with_index do |applicant, index| %>
  <tr>
    <td><%= index + 1 %></td>
    <td><%= link_to applicant.user.fullname, applicant.user %></td>
    <td><%= link_to "Reviews", applicant.build_review, "data-toggle" => "modal", "data-target" => "#detail#{applicant.id}" %></td>
    <td></td>

    <!--Modal-->
    <div class="modal fade" id="detail<%= applicant.id %>" role="dialog">
      <div class="modal-dialog">
        <!--Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close close_book_detail" data-dismiss="modal">&times;</button>
            <%= form_for(Review.new) do |f| %>
              <%= render 'shared/error_messages', object: f.object %>
              <div class="form-group row">
                <%= f.label :rating, class: 'col-sm-2 form-control-label' %>
                <div class="col-sm-6" id='star-rating'>
                </div>
              </div>
              <%= f.label :comment %>
              <%= f.text_area :comment, placeholder: "Please enter comment here." %>
              <%= f.submit "Save changes", class: "btn btn-primary" %>
            <% end %>
          </div>
        </div>
      </div>
    </div>
  </tr>
<% end %>

下面是模式框的HTML。

<div class="modal fade" id="detail4" role="dialog">
          <div class="modal-dialog">
            <!--Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close close_book_detail" data-dismiss="modal">&times;</button>

                <form class="new_review" id="new_review" action="/reviews" accept-charset="UTF-8" method=post></form>
                <div class="form-group row">
                <label class="col-sm-2 form-control-label" for="review_rating">Rating</label>
                <div class="col-sm-6" id="star-rating" style="cursor: pointer;">
                  <img alt="1" src="/assets/star-on.png" title="bad">
                  <img alt="2" src="/assets/star-on.png" title="poor">
                  <img alt="3" src="/assets/star-on.png" title="regular">
                  <img alt="4" src="/assets/star-off.png" title="good">
                  <img alt="5" src="/assets/star-off.png" title="gorgeous">

                  <input name="review[rating]" type="hidden" value="3">
                </div>

                <label for="review_comment">Comment</label>
                <textarea placeholder="Please enter comment here." name="review[comment" id="review_comment></textarea>

                <input type="submit" name=commit" value="Save changs" class="tbn btn-primary" data-disable-with="Save changes">
              </div>
            </div>
          </div>
        </div>

有解决方案吗?预先谢谢你。

2 个答案:

答案 0 :(得分:2)

通过target中的raty()选项可获得星级评分:-

<script>
  $('#star-rating').raty({
    path: '/assets',
    target     : '#id_of_field_to_pass_star_rating',
    targetType : 'score',
    targetKeep : true
  });
</script>

在模式中,创建一个隐藏字段,如下所示:-

<div class="col-sm-6" id='star-rating'>
<%=hidden_field_tag "review[rating]", '', id: "id_of_field_to_pass_star_rating" %>

您可以为jquery-raty以及宝石红宝石jquery-raty-rails

获得各种选择

答案 1 :(得分:0)

此解决方案是在单击星号时将比率分数值传递到隐藏的表单字段中。

  <div id="star-rating"></div>
  <%=hidden_field_tag "review[rating]", id: "review_rating" %>
  <script>
    $('#star-rating').raty({
      click: function(score) {
        $('#review_rating').val(score);
      }
    });
  </script>