2016-06-01 13 views
0

rankingは、ユーザーが時間順に最初のメモの下に保存した場合にのみ保存されます。オーバーライドされている属性をnilとして保存する方法は?

enter image description here

挑戦/

<% @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 %> 

ノート/フォーム

<%= 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> 

ノートから他のすべては、このようなtextとして保存されているだけではないranking示し。

答えて

1

ページに同じ名前のONEと1つのみのHTML IDのみが指定できます。あなたの場合、すべての非表示項目のIDは同じ#rankingです。

jQueryセレクタが最初を選択します。あなたはなぜこの問題に直面しているのか説明しています。

あなたの隠しフィールドにクラスを与え、クリック機能の中で、隠しフィールドのランキングを子供たちで検索してください。

+0

私はそのような単純な解決策を愛し、何時間も費やしたと思っています。あなたの助けをありがとう:) –

+1

これは助けて嬉しい! Btw、コードをかなり単純化する必要があります。あなたは太っている。また、ビューにスクリプトを入れたり、そこにスタイルを入れたりしないでください=)運が最高です! – Abdo

関連する問題