Forum: Ruby on Rails Nil object for nested resource

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D95d75d4cf5c2fcf530ebb2b02599b1f?d=identicon&s=25 Matthew Lang (Guest)
on 2007-06-20 16:39
(Received via mailing list)
Hi all,

I have 2 models with a standard 1 to many relationship.  I'm also
REST on the two models and have added the appropriate code to the
routes.rb file.

# scorecard.rb:
class Scorecard < ActiveRecord::Base
  has_many :attributes

# attribute.rb:
class Attribute < ActiveRecord::Base
  belongs_to :scorecard

# routes.rb:
map.resources :scorecards do |scorecard|
  scorecard.resources :attributes

My problem is that I want to show a single scorecard with all it's
attributes.  I then want to display a "New attribute" link to add a
attribute to the current scorecard.

# scorecards/show.rhtml:
<%= label_for("scorecard") %>
<%=h %><br />

<%= label_for("template") %>
<%=h @scorecard.template %>

<% for attribute in @scorecard.attributes %>
    <td><%=h %></td>
    <td><%=h attribute.weight %></td>
<% end %>
<%= link_to "New Attribute", new_attribute_path(@scorecard) %>

However I get the message for line 17 (last line) when I try to view
this page,

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym

I have tried checking against various REST and Rails books and
and I thought I had used the correct syntax for creating a link to a
attribute from this scorecard.

Any help on this would be greatly appreciated.


This topic is locked and can not be replied to.