javascript - How to save attribute which is being overridden as nil? -
the ranking
saves if user saved under note first in chronological order.
challenges/show
<% @challenge.dates_challenged.first(@challenge.days_challenged + @challenge.missed_days).each_with_index |date, i| %> <% if @notes.any? { |note| note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") } %> <% @notes.each |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 <%= + 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) %> <%= 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 <%= + 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] |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 note being saved such text
, not ranking
.
you can have 1 , 1 html id same name on page. in case, hidden fields have same id #ranking
.
your jquery selector pick first. explains why you're facing issue.
just give hidden field class , inside click function, search children ranking hidden field.