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.
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.
On 9/11/07, dodgyboz [email protected] wrote:
In my controller i have
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.