If number of records = zero, display 'no records found'

[I’ve no idea how this got into the ‘test’ section so sorry for the duplicate!]

I have a list of scripts, that when one is clicked on displays a new
page with relating runs for that script.

I would like to include an if/else statement so that when the number of
runs for a script is zero, the resulting page displays ‘no runs found’.

The runs display uses a <% for run in @runs %> loop to display a rows of
run details, immediately after which I have placed <% if (@runs.size ==
0) %> - however, the ‘else’ part of the statement is still run even when
no rows are found.

What am I doing wrong?

On Jul 13, 2007, at 6:14 AM, Paul N. wrote:

run details, immediately after which I have placed <% if
(@runs.size ==
0) %> - however, the ‘else’ part of the statement is still run even
when
no rows are found.

What am I doing wrong?

Well, for starters, not showing enough of your code, but I’ll take a
shot anyway.

<% if @runs.blank? -%>

No rows found

<% else -%> <% for run in @runs -%> <%= row_for(run) %> <% end -%> <% end -%>

Since .blank? works with nil, it avoids possible issues with nil.size
== 0 (which is better expressed as @runs.empty? anyway).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

I’ve tried the “.blank?” addition and rails just seems to ignore it for
some reason.

Are the minuses in the -%> imperative btw? I’ve not seen them before.

Either way, it doesn’t seem to be working so can you suggest a way to
debug this to see where i’m going wrong?

Paul N. wrote:

I’ve tried the “.blank?” addition and rails just seems to ignore it for
some reason.

Are the minuses in the -%> imperative btw? I’ve not seen them before.

Either way, it doesn’t seem to be working so can you suggest a way to
debug this to see where i’m going wrong?

Your ‘if’ needs to be outside the ‘for loop’. Otherwise there is nothing
to iterate and the if statement is never hit.

Hope this helps.

Jean N. wrote:

Paul N. wrote:

I’ve tried the “.blank?” addition and rails just seems to ignore it for
some reason.

Are the minuses in the -%> imperative btw? I’ve not seen them before.

Either way, it doesn’t seem to be working so can you suggest a way to
debug this to see where i’m going wrong?

Your ‘if’ needs to be outside the ‘for loop’. Otherwise there is nothing
to iterate and the if statement is never hit.

Hope this helps.

OH, and <%= -%>, the - part means to NOT put a linefeed after the value
being displayed.

On 7/25/07, Paul N. [email protected] wrote:

Either way, it doesn’t seem to be working so can you suggest a way to
debug this to see where i’m going wrong?

Post your code. Otherwise, everyone’s just guessing.

Bob S. wrote:

On 7/25/07, Paul N. [email protected] wrote:

Either way, it doesn’t seem to be working so can you suggest a way to
debug this to see where i’m going wrong?

Post your code. Otherwise, everyone’s just guessing.

Jean’s first post identified the problem - thanks Jean :slight_smile: