【问题标题】:How to render only notes that equal date?如何仅呈现相同日期的笔记?
【发布时间】:2016-05-03 20:42:11
【问题描述】:

除了每天显示所有笔记,我们如何每天只显示一个笔记,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

【问题讨论】:

  • 您能解释一下为什么您提出的解决方案不起作用吗?是否可以提供错误消息或不正确的输出?编辑问题比在 cmets 中回复要好。谢谢!
  • 嗨@MichaelGaskill。我没有提出解决方案。我解释了我所拥有的,每个音符都用notes/notes 呈现。我不希望每一个音符都被渲染。我只想注意,等于 date 将在相应的日期呈现

标签: ruby-on-rails ruby


【解决方案1】:
<% 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

【讨论】:

  • 打我一拳。刚刚想通了,但你的答案更简洁。谢谢!
【解决方案2】:

将此&lt;%= render 'notes/notes' %&gt; 部分更改为以下一个,然后按照note.html.erb 文件的正下方。

挑战秀:

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

将 note.html.erb 重命名为下划线 _notes.html.erb

notes/_notes.html.erb:

  <%= note.notes_text %>

【讨论】:

  • 这如何回答 OP 提出的问题?
  • 你不喜欢什么?我虽然,他试图通过部分使用该笔记。另外,正如我所见,他更新了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2011-07-30
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多