2016-05-03 24 views
0

而不是每天顯示所有的筆記,我們如何才能每天只顯示一個筆記,其筆記note.note_date == date如何渲染只有相同日期的筆記?

Day 1: Notes/notes # Shows both Day 1 and Day 3 note. Only want to show Day 1 note 
Day 2: Notes/form 
Day 3: Notes/notes # Shows both Day 1 and Day 3 note. Only want to show Day 3 note 
Day 4: Notes/form 
Day 5: Notes/form 

挑戰/顯示

<% @challenge.dates_challenged.first(@challenge.days_challenged).each_with_index do |date, i| %> 
    Day <%= i + 1 %> 
    <% if @notes.any? { |note| note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") } %> 
    <%= render 'notes/notes' %> 
    <% else %> 
    <%= render 'notes/form', :date => date %> 
    <% end %> 
<% end %> 

筆記/筆記

<% @notes.each do |note| %> 
    <%= note.notes_text %> 
<% end %> 

票據/表格

<%= form_for [@notable, @note] do |f| %> 
    <%= f.text_area :notes_text, placeholder: 'Enter Recap' %> 
    <%= f.date_select :notes_date, selected: date, :order => [:month, :day, :year] %> 
<% end %> 

challenges_controller

def show 
    @notable = @challenge 
    @notes = @notable.notes 
    @note = Note.new 
end 

總括來說,用戶創建一個挑戰。挑戰有屬性days_challenged。用戶選擇將要挑戰多少天,即10,15,30等。對於這些天中的每一天,將呈現notes/form。如果note.notes_date等於相應的一天,那麼我們如何才能僅顯示一張音符來代替notes/form

+0

您能解釋爲什麼您提供的解決方案不起作用嗎?是否有可以提供的錯誤信息或不正確的輸出?對這個問題的編輯比在評論中回覆要好。謝謝! –

+0

Hi @MichaelGaskill。我沒有提出解決方案。我解釋了我有什麼,每個音符都是用'notes/notes'來表示的。我不希望每個音符都被渲染。我只需要注意,等於'日期'將在當天呈現 –

回答

2
<% if @notes.any? 
    @today_notes = @notes.select{ |note| note.notes_date.strftime("%m/%d/%y") == date.strftime("%m/%d/%y") } %> 
    <%= render 'notes/notes' %> 
<% else %> 
... 
<% end %> 

然後使用@today_notes部分在筆記/筆記中。

+0

打我一拳。只是想出來,但你的答案更簡潔。謝謝! –

0

將此<%= render 'notes/notes' %>更改爲低於1,並且緊跟在note.html.erb文件的後面。

挑戰顯示:

<%= render partial: 'notes/notes', locals:{note: note} %> 

重命名note.html.erb強調_notes.html.erb

筆記/ _notes.html.erb:

<%= note.notes_text %> 
+0

這是如何回答OP提出的問題? – Alfie

+0

你不喜歡它?儘管如此,他試圖通過部分使用該音符。另外,因爲我看到他更新了這個問題。 – 7urkm3n