Forum: Ruby on Rails check for empty? in the view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Martyn E. (Guest)
on 2008-12-03 15:59
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
Frederick C. (Guest)
on 2008-12-03 16:06
(Received via mailing list)
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
Martyn E. (Guest)
on 2008-12-03 16:30
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:-

<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
mahmoud said (Guest)
on 2008-12-03 16:38
(Received via mailing list)
On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard <
removed_email_address@domain.invalid> 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">



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


--
Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
+20-16-1223857
Martyn E. (Guest)
on 2008-12-03 17:22
mahmoud said wrote:
> On Wed, Dec 3, 2008 at 4:30 PM, Martyn Elmy-liddiard <
> removed_email_address@domain.invalid> 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 (Guest)
on 2008-12-03 17:30
(Received via mailing list)
On Wed, Dec 3, 2008 at 5:22 PM, Martyn Elmy-liddiard <
removed_email_address@domain.invalid> 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%></option>
>
> Martyn
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
+20-16-1223857
This topic is locked and can not be replied to.