0

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.

fuzzyalej
  • 5,903
  • 2
  • 31
  • 49
Bryan Parman
  • 115
  • 1
  • 7

1 Answers1

0

@submissions always displays the submission of the first day because you've never changed it after you initialized it in the controller. Incrementing @num_days_ago in the view does nothing because the view does not use it.

If you want to display submissions since the first day, you need to set an array of submission from the first day to @submissions.

@submissions = num_days_since_first_day.times.map do |num_days_ago| 
  Submission.daily(num_days_ago).paginate(page: params[:page], per_page: 10).order('rank DESC')
end

And render each of them as you iterate in the view.

<%= render @submissions[num] %>

I think you can remove some redundant code and further refactor the code above, but that's the basic idea.

tyamagu2
  • 969
  • 7
  • 17
  • I think this is definitely on the right path. I'm getting an ```'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.``` error after implementing. I think this has to do with the fact that I'm trying to render @submissions[num]. @submissions[num] seems like a different type of object then the @submissions I had prior. – Bryan Parman Jan 20 '15 at 02:24
  • I guess there are days where no submissions were made and thus @submissions[num] is nil. Adding `next unless @submissions[num].present?` before the render might fix the issue. – tyamagu2 Jan 20 '15 at 04:08
  • That did the trick. The way I have this set up seems fragile, but it works for now! – Bryan Parman Jan 20 '15 at 17:47