【发布时间】:2019-06-28 08:43:20
【问题描述】:
我是 Rails 的新手,我正在使用 Star Rating jQuery Plugin Raty 来做一些评论/评级事情。现在我有一个需要审查的数据列表,然后审查的方法将在一个 boostrap 模态框中,如下图所示。
一切正常,但是当我点击图片中的提交按钮(显示保存更改)时,我没有收到评级参数,我收到的参数如下图所示。
预期结果也将包括评分值。我已按照步骤在 application.js 中添加 jquery.raty,并在 vendor/assets/javascripts 中分配文件。我还在我的文件中添加了这个 javascript 代码。
<script>
$('#star-rating').raty({
path: '/assets',
scoreName: 'review[rating]'
});
</script>
一切正常,只是没有通过评级值。我检查了结果,发现该值实际上保存在一个 hidden_tag 中,它包含在 jQuery 插件中,但是当我提交表单时它没有通过。
这是我的表格和模态框代码
<% @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">×</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">×</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>
有没有办法解决这个问题?提前谢谢你。
【问题讨论】:
-
能否添加生成的 html 用于评分输入?
-
嗨 Vasilisa,我已经更新了问题,提前谢谢你。此外,我尝试插入使用 link_to 重定向到另一个页面但不使用模式框的评论,然后一切正常。你对此有什么想法?
-
由于某种原因,
form标记在启动后立即关闭,它不包括表单本身。我想这是因为表格里面的模态。div和form标签在tr标签内无效。不知道怎么解决 -
是的,我注意到了,如果我使用 number_field 进行评分,那就没问题了。所以我不知道如何处理这个问题。而且 div 和 form 标签只适用于模态框,这也无效?
标签: jquery ruby-on-rails rating raty