Listing objects under the date they were created

不羁的心 提交于 2019-12-13 05:59:53

问题


Sorry for the amateur question but I am still quite new to rails. I currently have an app that creates jobs and I would like to display the jobs beneath the date they were created in the same way they do on Dribble

At the moment to display the jobs I have te following:

 <% @jobs.each do |job| %>
   <div class="job-wrapper"><%= link_to user_job_path(job.user_id ,job) do %>
    <div class="job">
     <div class="job-desc"><strong><%= job.company %></strong> are looking for a <strong><%= job.job_title %></strong>
      <div class="job-sal"><%= job.job_salary %></div>
     </div>
    </div>
   </div>
  <% end %>
  <% end %>

I am sure I need to create a loop of some kind to make this work but am unsure how to incorporate it so that the date only displays once at the top of the jobs and then all jobs created during that date are shown?

Any help would be much appreciated! Thanks


回答1:


Try the pattern below --

<% @job.group_by{|x| x.created_at.to_date }.each do |date,jobs_on_that_date| %>
  <%= date %>
  <% jobs_on_that_date.each do |job| %>
    # render the job
  <% end %>
<% end %>

Basically you need to group your jobs by the date (or whatever you want to group on) then get a hash keyed on the stuff you grouped on. Then render the key (date) followed by the list of objects relating to that key.



来源:https://stackoverflow.com/questions/11720513/listing-objects-under-the-date-they-were-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!