Hi all, I have a table like the one below called key_value_pairs id spec val 1 size small 2 size medium 3 size small In my controller i have def edit @pair = KeyValuePair.find(params[:id]) end and in my view i have such code as <%= @pair.val %>. This code was all working as expected before (before what i''m not sure, but now it no longer works) as far as im aware i have made no changes to the edit method in the controller or any changes to the view and all the data is still in the database table but now when i try and view the page i get nil object error message You have a nil object when you didn''t expect it! The error occurred while evaluating nil.val Does any one have any suggestions or ideas? has this happened to anyone else? I am not sure how to proceed as all my code (which is rather simple) looks like it should work. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
nil object is what you get when the you get no results. Check for nil in your view, or adjust your controller to not send a nil. <%= @pair.val unless @pair.nil? -%> OR # Don''t set @pair to a nil, ever def edit value = KeyValuePair.find_by_id(params[:id]) @pair value || KeyValuePair.new # You''ll get an empty object if value is nil. end dodgyboz wrote:> Hi all, > I have a table like the one below called key_value_pairs > > id spec val > 1 size small > 2 size medium > 3 size small > > > In my controller i have > > def edit > @pair = KeyValuePair.find(params[:id]) > end > > and in my view i have such code as <%= @pair.val %>. > > This code was all working as expected before (before what i''m not > sure, but now it no longer works) as far as im aware i have made no > changes to the edit method in the controller or any changes to the > view and all the data is still in the database table but now when i > try and view the page i get nil object error message > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.val > > Does any one have any suggestions or ideas? has this happened to > anyone else? > > I am not sure how to proceed as all my code (which is rather simple) > looks like it should work.-- 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 -~----------~----~----~----~------~----~------~--~---
On 9/11/07, dodgyboz <dodgyboz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > I have a table like the one below called key_value_pairs > > id spec val > 1 size small > 2 size medium > 3 size small > > > In my controller i have > > def edit > @pair = KeyValuePair.find(params[:id]) > end > > and in my view i have such code as <%= @pair.val %>. > > This code was all working as expected before (before what i''m not > sure, but now it no longer works) as far as im aware i have made no > changes to the edit method in the controller or any changes to the > view and all the data is still in the database table but now when i > try and view the page i get nil object error message > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.val > > Does any one have any suggestions or ideas? has this happened to > anyone else?There''s not enough information to know what the problem is. The find() should raise an exception if the record isn''t found, so I suspect that for some reason, the find() isn''t being performed, and thus @pair isn''t being set. One quick thing you can do is to temporarily add this to your controller right after the find: raise @pair.inspect Then call your action from the browser. If you still get the view error, then the find didn''t happen. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---