简体   繁体   English

如何保存被覆盖为nil的属性?

[英]How to save attribute which is being overridden as nil?

The ranking only saves if the user saved it under the note that was first in chronological order. 仅当用户将其保存在按时间顺序排列的第一个音符下时,该ranking才会保存。

在此处输入图片说明

challenges/show 挑战/表演

<% @challenge.dates_challenged.first(@challenge.days_challenged + @challenge.missed_days).each_with_index do |date, i| %>
  <% if @notes.any? { |note| note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") } %>
    <% @notes.each do |note| %>
      <% if note.notes_text.present? %>
        <% if note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") %>
          <div class="notes-notes-background">
            <% if note.ranking == 1 %>
              <%= image_tag '1.png', class: 'note-emoticon' %>
            <% elsif note.ranking == 2 %>
              <%= image_tag '2.png', class: 'note-emoticon' %>
            <% elsif note.ranking == 3 %>
              <%= image_tag '3.png', class: 'note-emoticon' %>
            <% elsif note.ranking == 4 %>
              <%= image_tag '4.png', class: 'note-emoticon' %>
            <% end %>
            <div style="font-weight: bold; font-size: 14px; color: #a0b4f0;">DAY <%= i + 1 %></div>
            <b><%= note.notes_date.strftime("%b %d, %Y") %></b><br><br>
            <div style="margin-left: 20px; margin-right: 20px;">
              <%= link_to edit_note_path(note) do %>
                <%= simple_format note.notes_text %>
              <% end %>
            </div>
          </div>
        <% end %>
      <% else %>
      <% end %>
    <% end %>
  <% else %>
    <div class="notes-form-background" style="padding-top: 1em;">
      <div style="margin-bottom: -19px; color: #a0b4f0; font-weight: bold; font-size: 14px; color: #a0b4f0;">DAY <%= i + 1 %></div><br>
      <div style="color: #eee;"><%= date.strftime("%b %d, %Y") %></div>
      <div style="margin-left: 20px; margin-right: 20px;">
        <%= render 'notes/form', :date => date %>
      </div>
    </div>
  <% end %>
<% end %>

notes/form 笔记/表格

<%= form_for [@notable, @note] do |f| %>
  <%= f.hidden_field(:ranking, id: 'ranking') %>
  <%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker') %>
  <%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker') %>
  <%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker') %>
  <%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker') %>
<% end %>

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    $('.clicked').removeClass('clicked')
    $(this).addClass('clicked');
  });
</script>

Everything else from the note is being saved such as the text , just not the ranking . 笔记中的其他所有内容都将被保存,如text ,而不是ranking

You can only have ONE and only ONE html id with the same name on a page. 您在页面上只能使用一个,并且只能使用一个具有相同名称的html id。 In your case, all your hidden fields have the same id #ranking . 在您的情况下,所有隐藏字段都具有相同的ID #ranking

Your jQuery selector will pick the first. 您的jQuery选择器将选择第一个。 Which explains why you're facing the issue. 这就解释了您为什么要面对这个问题。

Just give your hidden field a class and inside your click function, search the children for the ranking hidden field. 只需给您的隐藏字段提供一个类,然后在您的click函数中,在子级中搜索排名隐藏字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM