I'm fairly new to rails, and working on a application that would display daily submissions. I'm now trying to display posts for every day. So I did a few things:
Set up the controller instance variable to a single @submissions, which takes the 'num_days_ago' input:
@num_days_ago = 1
@submissions = Submission.daily(@num_days_ago).paginate(page: params[:page], per_page: 10).order('rank DESC')
Set up the scope to be responsive to the num_days_ago input:
scope :daily, -> (num) { where( created_at: (num.days.ago.beginning_of_day..num.days.ago.end_of_day)) }
Added some helper methods to handle the dates in the views:
module WelcomeHelper
def the_date(num_days)
date = num_days.days.ago.to_date
date.to_formatted_s(:long_ordinal) end
def num_days_since_first_day
(Date.today - (Submission.last.created_at).to_date ).to_i end
end
Created a loop in the view to make it display all daily posts (I will add pagination and infinite scroll later):
<div class="container">
<% (0..num_days_since_first_day).each do |num| %>
<h3><%= the_date(num) %></h3>
<ul>
<%= render @submissions %>
</ul>
<div id="show_more">
<%= link_to 'Show More', welcome_index_path(page: 2), class: "show_more_link", remote: true %>
</div>
<% @num_days_ago +=1 %>
<% end %>
</div>
Also, here is the submissions partial being displayed in the Index view:
<li>
<%= render partial: 'votes/voter', locals: { submission: submission } %>
<%= submission.title %></br>
<%= submission.description %> | $<%= submission.price %> | <b><%= submission.merchant.name %></b> | Submitted by: <%= submission.user.name %>
</li>
</br>
So now, the issue I'm having is that the @submissions instance variable will always be the first day. So I'm trying to figure out how to get this to be responsive to the @num_days_ago which is being updated += 1 after leach loop. Any suggestions on how to make this happen?
Right now, it's displaying all of the days that have had posts since the first day a post was created, but the posts displayed for each day are all the 12 posts that were created on the first day.