本文介绍了如果条件在每个做 Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要打印出 active 为 == 0 的候选者,这是我在视图中的代码.

如果 active 是 yes 或 no,我可以打印.. 但是在每个 do 循环中,我只想打印 active 候选.

那么如何将条件添加到我的每个 do 循环中,谢谢.

 <%其他%><%=t('generales.noo')%><%结束%>

<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')}%><%结束%>

<%结束%>

<%结束%>

我已经试过了

我所有的想法都没有运气语法错误:P

解决方案

如果 candidate.active 实际上是一个布尔值,那么你可以说:

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

如果 @candidates 实际上是一个 ActiveRecord::Relation 那么你可能会说:

 false).each do |candidate|%>...<%结束%>

避免在你不想要的时候从数据库中取出一堆东西.

如果 active 实际上是一个数字(在数据库内和数据库外),那么你可以说:

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

 0).each do |candidate|%>...<%结束%>

Hi i need to print out just the candidates where active is == 0 here is my code in the view.

I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.

So how can i add the condition to my each do loop, thanks.

  <% @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

解决方案

If candidate.active is actually a boolean then you could say:

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

If @candidates is actually an ActiveRecord::Relation then you could probably say:

<% @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:

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

or

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

这篇关于如果条件在每个做 Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:12