Q:

Why won't my viewer display the results of the method?

happy is a integer variable that I have set as either 0 or 1. If it's 0 I want to display "No" and if it's 1 I want to display "Yes"
In my controller I have:
helper_method :happy_to_text
def happy_to_text(happy)
  happy > 0 ? 'Yes' : 'No'
end

def edit
  @todo_item = @todo_list.todo_items.find(params[:id])
end

If I try:
<td><%= happy_to_text(1) %></td>

it works correctly, displaying "Yes"
If I try:
<td><%= todo_item.happy %></td>

it works correctly, displaying "1"
If I try:
<td><%= happy_to_text(@todo_item.happy) %></td>

It fails with the error:
"undefined method `>' for nil:NilClass"

I'm sure I'm missing something basic, but I'm stuck. Any help would be appreciated.

A:

The object todo_item is nil because no value has been assigned to it.  It appears that you're attempting to use it in an ERB view, which probably means that it's intended to come from the database.
You should have a statement in your controller action (the one for the view) that does something like this:
def show
  @todo_item = TodoItem.find(params[:id])
end

It's possible that in your view code, you're just missing the @ prefix for the variable when you use it in your view.  In that case, you probably meant the code to look like this:
<td><%= happy_to_text(@todo_item.happy) %></td>

Otherwise, you're missing the code to assign a value to todo_item, and you'll need to do that before you can attempt to use the value.

