jeudi 9 mars 2017

Rails ignores if and else statement

First thing, sorry for my bad english.

I have a condition that put a string in a variable in my view.

My controller

    def  index
    @hosts= Host.all

    @hosts.each do |host|
      if host.ativo?
        @status = "Sim"
      else
        @status = "Não"
      end

      if Time.zone.now > host.validade
        flash[:info] = "Você tem hosts expirados !"
        @alerta = "Expirado !"
      else
        @alerta = "Válido !"
      end
     end
    end

My view

<tbody>
    <% @hosts.each do |host| %> 

      <tr>
        <td><%= link_to host.id, host_path(host),class: 'btn btn-default btn-xs'%></td>
        <td><%= host.mac %></td>
        <td><%= host.nome %></td>
        <td><%= host.descricao %></td>
        <td><%= @status %></td>
        <td><%= host.validade.strftime("%d/%m/%Y %H:%M")%></td>
        <td><%= @alerta %></td>
        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_host_path(host), :class => 'btn btn-default btn-xs' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      host_path(host),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-xs btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>
</div>

But in my @alerta varibale i only get the string "Expirado" and in @status i only get "Sim" even if the @host.ativo? return false and Time.zone.now its < than host.validade

Aucun commentaire:

Enregistrer un commentaire