On Rails 4.我试图用Bootstrap做一个复杂的嵌套循环。Ruby on Rails - 嵌套循环(带Bootstrap)
我有模型用户,组织和提交。提交内容属于用户和组织 - 当前用户创建新提交并将其分配给组织(所以,提交具有属性:user_id
和:organization_id
)。
我做了一个小图片来说明什么,我想在提交索引视图做:
列出当前所有用户提交的:
基本上,我需要找到每个组织/年当前用户已经分配了一个提交,然后在该标题内,将每个当前用户的提交归入同一组织/年。
为了使用Bootstrap获得此格式,我需要有一个.in_groups_of(3, false)
声明(除非有人能告诉我有关不同的东西)与<div class="col-md-4">
布局。这里是我当前的代码在提交索引视图,这是不按预期工作:
# find all submissions made by the current user, group by the
# submission's :contest_year and :organization_id attributes:
<% current_user.submissions.group([:contest_year, :organization_id]).each do |contest| %>
<h2><%= contest.contest_year %>: <%= contest.organization.name %></h2>
# here is where it fails, it is finding all the submissions made by the user
# instead of just the submissions that belong under the
# specified contest_year/organization_id heading from above.
# I know I need 'contest' involved in here
<% current_user.submissions.in_groups_of(3, false).each do |group| %>
<div class="row">
<% group.each do |submission| %>
<div class="col-md-4">
# submission information here
</div>
<% end %>
</div><br/>
<% end %>
<% end %>
此代码返回这样的事情,而不是:
我希望这一切才有意义。那么,我如何修复嵌套循环,以便它像第一个图像那样工作。非常感谢。部分提交模型/控制器:
Submission.rb
belongs_to :user
belongs_to :organization
submissions_controller.rb
def index
@submissions = Submission.all
end
哦,我的天哪!所以,我在几次之前以几乎不同的方式做了几次声明......但是我在做current_user.submissions.where(:organization_id == contest.organization_id)! (这不起作用)这整个时间都是语法问题,啊!要获得这两个过滤器,它将是current_user.submissions.where(organization_id:contest.organization_id,contest_year:contest.contest_year),对不对? – Rachel9494