【问题标题】:How to store multiple values from star ratings using Jquery Raty in Rails如何在 Rails 中使用 Jquery Raty 存储来自星级评分的多个值
【发布时间】:2019-11-03 11:38:16
【问题描述】:

我正在构建一个应用程序,用户可以在其中根据一组标准对列表进行评分。

我希望用户能够给每个属性评分 1-5 星。

示例: 位置(1-5 星) 物有所值(1-5星) 等等。

我目前有一个看起来像这样的简单表单。

<%= simple_form_for([@stadium, @rating]) do |f| %>

<label>Rate the Location:</label>
<div class="rating-location">
</div>


<label>Rate the value for the money:</label>
<div class="rating-monetary-value">
</div>

...

<div class="actions">
  <%= f.button :submit,  "Submit Rating", class:'btn btn-success' %>
</div>
<% end %>
<script>
  $('.rating-location').raty({
    path: '/assets/',
    scoreName: 'rating[location]',
    hints: ['bad', 'poor', 'regular', 'good', 'gorgeous']
    });
</script>

这会将位置评分保存到我的评分表中的位置列中。

但我不确定我应该如何进行才能将超过 1 个值保存到同一个表中。例如货币价值评级和其他评级属性)。总共有 8 种不同的属性可供用户评分。

我是 Rails 和 Jquery 的新手,正在努力学习。任何帮助将不胜感激。

【问题讨论】:

    标签: jquery ruby-on-rails


    【解决方案1】:

    我遇到了与您类似的问题。我的解决方法是为每个属性创建单独的 id 并在 js 脚本中创建单独的比率调用。所以在你的情况下:

    <label>Rate the Location:</label>
     <div id="rating-location"></div>
    
    <label>Rate the Monetary Value:</label>
    <div id="rating-monetary-value"></div>
    
    <script>
      $('#rating-location').raty({
        path: '/assets/',
        scoreName: 'rating[location]',
        hints: ['bad', 'poor', 'regular', 'good', 'gorgeous']
      });
    
      $('#rating-monetary-value').raty({
        path: '/assets/',
        scoreName: 'rating[monatery-value]',
        hints: ['bad', 'poor', 'regular', 'good', 'gorgeous']
      });
    </script>
    

    这似乎有很多代码重复(我有大约 10 个属性),但它现在有效。

    您还可以使用 gem "ratyrate" 来指定模型中的每个属性:https://github.com/wazery/ratyrate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多