weixin_33696822 2016-12-21 14:22 采纳率: 0%
浏览 22

铁轨星级评定系统

In my project I want to use a rating system http://www.jqueryrain.com/?SGgGB_oZ I have successfully added it to my project, and it works. I have 2 models: Casino & Rating. Casino has_many :ratings, and Rating belongs_to :casino. On my casino index page I want to display the rating system for each casino. This is my view:

<h1>List of casinos</h1>

<div class="casinos-list">
  <% @casinos.each do |casino| %>
    <div class="casino-item">
      <p class="casino-name"><%= link_to casino.name, casino %></p>
      <p class="casino-description"><%= casino.description %></p>
      <ul class="rating">
        <% form_id = "casino_#{casino.id}_rating" %>
        <%= form_for casino.ratings.last || casino.ratings.build, html:
            { id: "casino_#{casino.id}_rating", class: 'star_rating_form' } do |f| %>
          <%= f.hidden_field :casino_id %>
          <%= f.hidden_field :score, id: "#{form_id}_stars", class: 'star-value' %>
        <% end %>

      </ul>
      <div id="<%= "average_rating_#{form_id}" %>"><%= casino.average_rating.to_f %></div>
      <div id="<%= "rate_#{casino.id}" %>" class="rateit" data-rateit-mode="font">
      </div>
    </div>
  <% end %>
</div>

My application.js:

$(document).on('turbolinks:load', function () {

  $('.rateit-reset').remove();

  $('.rateit-hover').click(function () {
    var rating = $(this).parents().attr('aria-valuenow');
    var float_number = parseFloat(rating);
    var rating_form_input = $(this).parents().parents('.casino-item').children('.rating').children('.star_rating_form').children('.star-value');
    var form_id = $(this).parents().parents('.casino-item').children('.rating').children('.star_rating_form').attr('id');
    rating_form_input.val(float_number);

    $.ajax({
      type: 'post',
      url: $('#' + form_id).attr('action'),
      data: $('#' + form_id).serialize()
    });
  });

});

And it works, saving the proper value to db, but, as you can see, I use here form_for casino.ratings.last. I need to add a new rating to the array of ratings, but not to change the last one. Any ideas how should I do it? Thanks.

  • 写回答

1条回答

  • helloxielan 2016-12-21 14:42
    关注

    If you want just add a new rating, remove casino.ratings.last. keep only casino.ratings.build

    评论

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题