# Error: undefined method for nil:NilClass

**URL:** <https://www.ruby-forum.com/t/error-undefined-method-for-nil-nilclass/183785>\
**Category:** Rails\
**Created:** [February 20, 2010, 12:58am UTC](https://www.ruby-forum.com/t/error-undefined-method-for-nil-nilclass/183785 "2010-02-20T00:58:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@jeff-k](https://www.ruby-forum.com/u/jeff-k)\
**Post date:** [February 20, 2010, 12:58am UTC](https://www.ruby-forum.com/t/error-undefined-method-for-nil-nilclass/183785/1 "2010-02-20T00:58:45Z")

</div>

When I test functionals, I get the following error:

```
test_should_display_index(AlertsControllerTest):
ActionView::TemplateError: undefined method `name' for nil:NilClass
    On line #9 of app/views/alerts/index.html.erb

        6:
        7: <tr class="<%= row_class %>">
        8: <td scope="row" class="first
odd"><%=h alert.title %></td>
        9: <td class="even"><%=h
alert.file_status.name %></td>
        10: <td class="last odd"><%= render
:partial => "shared/action_links", :locals => { :obj
=> alert, :type => "alert" } %>
        11: </td>
        12: </tr>

```

This test should confirm the proper display of the index for class  
Alerts. Alerts belong to class FileStatuses. Each file\_status contains  
two objects: :id and :name.

`<%=h alert.file_status.name %>` produces the expected  
result when viewed in a browser (either `draft`, `published` or `trash`  
depending on the individual alert's setting). So it seems that the  
only thing finding this error is the test.

The test is pretty straight forward.&nbsp; Its code is:

```
class AlertsControllerTest < ActionController::TestCase

  test "should display index" do
    get :index
    assert_response :success
    assert_template 'index'
    assert_not_nil assigns(@alert)
  end
end
```

So what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![jeff-k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@jeff-k](https://www.ruby-forum.com/u/jeff-k)\
**Post date:** [February 20, 2010, 1:00am UTC](https://www.ruby-forum.com/t/error-undefined-method-for-nil-nilclass/183785/2 "2010-02-20T01:00:20Z")

</div>

and of course, the source code is not escaped…

---

<div class="post-metadata">

**Author:** ![jeff-k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@jeff-k](https://www.ruby-forum.com/u/jeff-k)\
**Post date:** [February 20, 2010, 1:53am UTC](https://www.ruby-forum.com/t/error-undefined-method-for-nil-nilclass/183785/3 "2010-02-20T01:53:55Z")

</div>

OK, I figured it out. I just needed to populate my fixtures (in  
/tests/fixtures/). There is a good videocast addressing how to represent  
these relationships here:

> **[\#81 Fixtures in Rails 2.0 - RailsCasts](http://railscasts.com/episodes/81-fixtures-in-rails-2-0)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
