简体   繁体   English

如果每个条件都做Rails

[英]If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view. 嗨,我只需要打印active == 0的候选者,这是我在视图中的代码。

I can print if active is yes or no.. But in the each do loop i just want to print the active candidates. 如果活动是是或否,我可以打印。但是在每个do循环中,我只想打印活动候选者。

So how can i add the condition to my each do loop, thanks. 那么,如何将条件添加到我的每个do循环中,谢谢。
  <% @candidates.each do |candidate| %> <div id="candidateper"> <div class="avatth" ><div class="avat_min"> <% if candidate.avatar.present? %> <%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %> <% else %> <%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %> <% end %> </div></div> <div class="nameth"><%= candidate.name %></div> <div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div> <div class="generalth"> <% if candidate.user.purchased_at.present? %> <%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %> <% else %> <%= t('generales.noo') %> <% end %> </div> <div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %> <% if current_user.user_type == 'admin' %> <%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %> <% end %> </div> </div> <% end %> </div> <% end %> 

I`ve tried this 我试过了

no luck syntax error on all my ideas :P 我所有的想法都没有运气语法错误:P

If candidate.active is actually a boolean then you could say: 如果candidate.active实际上是布尔值,那么您可以说:

<% @candidates.reject(&:active).each do |candidate| %>
    ...
<% end %>

If @candidates is actually an ActiveRecord::Relation then you could probably say: 如果@candidates实际上是ActiveRecord::Relation那么您可能会说:

<% @candidates.where(:active => false).each do |candidate| %>
    ...
<% end %>

to avoid pulling a bunch of stuff out of the database when you don't want it. 避免在不需要时从数据库中拉出一堆东西。

If active is actually a number (inside the database and outside the database) then you could say: 如果active实际上是一个数字(在数据库内部和数据库外部),那么您可以说:

<% @candidates.select(&:zero?).each do |candidate| %>
    ...
<% end %>

or 要么

<% @candidates.where(:active => 0).each do |candidate| %>
    ...
<% end %>

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

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