views:

146

answers:

4

I know this is probably rather trivial but i have had a lookt at previous questions and i've tried them but they still issued an error unfortunately :s

My issue is the following, i have an html.erb file and i want a certain body text to be display given a condition or another if it is false

i have

<% if [email protected] do %>

more code goes here

<% end %>

I have tried many combinations but the most frequent error i keep getting is

You have a nil object when you didn't expect it! The error occurred while evaluating nil.nil

I'm not sure what i'm doing wrong, this is most likely something very easy but i cant find my way through it! :s Any assistance would be really appreciated, thanks in advanced

A: 

Maybe try .nil? or == nil, or take a close look at your controller code that's assigning a value to @selector.

thenoviceoof
+3  A: 

There's no .nil method. You're thinking of .nil?. It should be [email protected]?.

Or you could get the same result from if @selector and it will evaluate to be the same as if [email protected]?. This is assuming you not referring to a boolean.

Jim
You're right on the first count, but not the second. Usually "if @selector" will be as good as "if [email protected]?" but to speak precisely they aren't equivalent--for example, if @selector is supposed to be boolean and the poster wants false and nil to give different results
Jordan
Jordan, you're right. I assumed he was referring to a record.
Jim
cheers! the .nil? worked great, i had it in some previous code version but i must have removed it when i tried some count or so! Thanks a lot!!
Erika
I updated it so nobody will get confused in the future. Thanks Jordan.
Jim
A: 

just

if @selector

is enough

Zepplock
A: 

In Rails, a more idiomatic way would be <% if @selector.present? do %> or in idiomatic Ruby <% unless @selector.nil? do %>

harrylove