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| %> <tr> <td><% docv.modifier %></td> <td><%= docv.comment %></td> <td><%= docv.policy.PolicyName if docv.policy_id %></td> <td><%= docv.standard.StandardsName if docv.standard_id %></td>

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

Thanks

<% @docversions.each do |docv| %> <tr> <td><% docv.modifier %></td> <td><%= docv.comment %></td> <td><%= docv.policy.PolicyName if docv.policy_id %></td> <td><%= docv.standard.StandardsName if docv.standard_id %></td>

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

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

Fred

Frederick Cheung 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:-

<tr> <td><select name="risk[LastReviewed(1i)]"> <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed.year %>" selected="selected"><%= @risks.LastReviewed.year %></option> <option value="2008">2008</option> .......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

Frederick Cheung 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:-

mahmoud said 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 <option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" selected="selected">

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

<option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>" selected="selected"><%= @risks.LastReviewed.year if @risks.LastReviewed%></option>

Martyn

mahmoud said 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

<option value="<%= @risks.LastReviewed.year if @risks.LastReviewed%>"

selected=“selected”>

– 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.

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 … and you can only call NilClass methods for it.