This looks wrong, but it works... what is right way?

<% if @base_coverages != nil %>
<% if @base_coverages.length > 0 %>

<% @base_coverages.each do |i| %> "> <% end %>
<%= i.coverage_id %> <%= i.external_description %>
<% end %> <% end %>
  • Base coverages may be nil
  • Base coverages may have a length of 0

How do I clean this code up?

Thanks,
Jason

Jason V. wrote:

<% if @base_coverages != nil %>
<% if @base_coverages.length > 0 %>

<% @base_coverages.each do |i| %> "> <% end %>
<%= i.coverage_id %> <%= i.external_description %>
<% end %> <% end %>
  • Base coverages may be nil
  • Base coverages may have a length of 0

How do I clean this code up?

Thanks,
Jason

@base_coverages ||= [] would take care of the nil case.

You may actually be able to use base_coverages.blank? which I think
works as expected for nil, empty strings, empty arrays etc etc

You could pull the

into a partial and end up with

<%= render :partial => “coverages” if !@base_coverages.blank? %>

2006/12/5, Jason Vogel [email protected]:

<% end %> <% end %>
  • Base coverages may be nil
  • Base coverages may have a length of 0

How do I clean this code up?

How about this (not much different):

<% if !@base_coverages.blank? %>

<% @base_coverages.each do |i| %> > <% end %>
<%= i.coverage_id %> <%= i.external_description %>
<% end %>

Regards,
Rimantas

http://rimantas.com/

<…>

You could pull the

into a partial and end up with

<%= render :partial => “coverages” if !@base_coverages.blank? %>

Good idea. I’d change a bit:

<%= render :partial => “coverages” unless @base_coverages.blank? %>

Regards,
Rimantas

http://rimantas.com/