Hi, I''m having a bit of an IF-statement-problem that produces a
NoMethodError if it''s included in my method, although there is a
corresponding column in my table. What I want to do is check if an
record exists, or else I would like to create a new one based on
previously defined variables in my method.
def ait
@country = ''Some place warm''
@location = Location.find_by_adress(@requested_adress)
if @location = nil
@new_location = Location.new
@new_location.adress = @requested_adress
@new_location.country = @hostname
@new_location.save
@location = @location_ip
end
render :text => @location.country
end
Without the if-statement, it all works fine but when it''s included I
get:
NoMethodError in TestsController#cached_hostname_reaction
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.country
It seems to me that I''m missing some fundamental Ruby-aspect, but I
can''t figure out what...
Many thanks!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Got it, by rewriting the if-statement like this:
if Location.find_by_adress(@requested_adress)
@location = Location.find_by_adress(@requested_adress)
else
@location = Location.new
@location.adress = @requested_adress
@location.country = @hostname
@location.save
end
Is there a more elegant way to do it?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
if @location.nil? not if @location = nil On Jan 15, 2008 1:21 PM, Gu stav <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, I''m having a bit of an IF-statement-problem that produces a > NoMethodError if it''s included in my method, although there is a > corresponding column in my table. What I want to do is check if an > record exists, or else I would like to create a new one based on > previously defined variables in my method. > > def ait > @country = ''Some place warm'' > @location = Location.find_by_adress(@requested_adress) > if @location = nil > @new_location = Location.new > @new_location.adress = @requested_adress > @new_location.country = @hostname > @new_location.save > @location = @location_ip > end > render :text => @location.country > end > > Without the if-statement, it all works fine but when it''s included I > get: > > NoMethodError in TestsController#cached_hostname_reaction > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.country > > It seems to me that I''m missing some fundamental Ruby-aspect, but I > can''t figure out what... > > Many thanks! > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---