Check for empty? in the view

I nned to get an associsted name from one table dependent on the related
id value in another table when rendering the view but cant figure out
the syntax to handle pontential empty values:-

i.e. docv.policy_id OR docv.standard_id will exist and therefore link
back to the policies OR the standards table respectively but ONLY ONE
will exist.

Therefore I need to check the existance of each field before attempting
to get the PolicyName OR StandrdsName from the related table otherwise
the empty item gives me a ‘.nil value’ error.

<% @docversions.each do |docv| %>

<% docv.modifier %> <%= docv.comment %> <%= docv.policy.PolicyName if docv.policy_id %> <%= docv.standard.StandardsName if docv.standard_id %>

Can someone advise the syntax to use in the view to achieve this. I have
also tried …“if !docv.standard_id.empty?”.

Thanks

On Dec 3, 1:59 pm, Martyn Elmy-liddiard <rails-mailing-l…@andreas-
s.net> wrote:

don’t think you need anything more than if docv.policy

Fred

Frederick C. wrote:

On Dec 3, 1:59?pm, Martyn Elmy-liddiard <rails-mailing-l…@andreas-
s.net> wrote:

don’t think you need anything more than if docv.policy

Fred

Thanks Fred but I still get an error:-

I gave a vey simple example and it may be fairer to give a slightly
different example that I am working on where the potential nil value is
the default selected value of a drop-down list:-

<%= @risks.LastReviewed.year %> 2008 .......etc

and the error I get is:-

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.year

On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard <
[email protected]> wrote:

Thanks Fred but I still get an error:-
…etc

and the error I get is:-

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.year

That’s because @risks.LastReviewed is the one that is nil
use

Posted via http://www.ruby-forum.com/.


Mahmoud Said
Software Developer

www.eSpace.com.eg
+20-16-1223857

mahmoud said wrote:

On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard <
[email protected]> wrote:

Thanks Fred but I still get an error:-
…etc

and the error I get is:-

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.year

That’s because @risks.LastReviewed is the one that is nil
use


Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
+20-16-1223857

Mahmoud,

Still not quite seeing why @risks.LastReviewed can be checked for
existance but @risks.LastReviewed.year cant be. I would have thought an
element of the date would be able to be checked in the same way as the
whole date.

Anyhow…you help was great and has allowed me to sort the problem so
thanks very much indeed.

Just for anyone else…I also needed to put the existance check in the
displayed element of the drop-down selection thus:-

<%= @risks.LastReviewed.year if @risks.LastReviewed%>

Martyn

On Wed, Dec 3, 2008 at 5:22 PM, Martyn Elmy-liddiard <
[email protected]> wrote:

existance but @risks.LastReviewed.year cant be. I would have thought an
element of the date would be able to be checked in the same way as the
whole date.

In @risks.LastReviewed.year you are calling the year method of the *
@risks.LastReviewed* object which, I assume, is of class Time . if
that
object is nil then it is no longer a Time object… it is a nil object
of
type NilClass http://www.ruby-doc.org/core/classes/NilClass.html
and
you can only call NilClass methods for it.

@risks.LastReviewed%>

Martyn


Posted via http://www.ruby-forum.com/.


Mahmoud Said
Software Developer

www.eSpace.com.eg
+20-16-1223857