简体   繁体   English

在Rails的视图中循环访问控制器实例变量

[英]Looping through a controller instance variable in a view in Rails

I'm fairly new to rails, and working on a application that would display daily submissions. 我是Rails的新手,正在开发一个可以显示每日提交内容的应用程序。 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: 将控制器实例变量设置为单个@submissions,它使用“ num_days_ago”输入:

@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: 将范围设置为响应num_days_ago输入:

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. 所以现在,我遇到的问题是@submissions实例变量将始终是第一天。 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. 所以我想弄清楚如何使它对@num_days_ago作出响应,而在浸出循环后将其更新+ = 1。 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. 现在,它将显示自创建帖子的第一天起所有已发布帖子的日子,但是每天显示的帖子是第一天创建的所有12条帖子。

@submissions always displays the submission of the first day because you've never changed it after you initialized it in the controller. @submissions始终显示第一天的提交,因为在控制器中对其进行初始化后从未更改过。 Incrementing @num_days_ago in the view does nothing because the view does not use it. 在视图中增加@num_days_ago不会执行任何操作,因为该视图未使用它。

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。

@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. 我认为您可以删除一些冗余代码并进一步重构上面的代码,但这是基本思想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM