0

The error message is NoMethodError in Circuit#update undefined method 'network_address' for nil:NilClass and related to this line in my view:

<td><%= logical_interface.subnet.network_address %></td>

Everything was working perfectly earlier and now I've managed to break it somehow when I restarted my local server.

update.rhtml

<table id="logical_interfaces">
    <% @logical_interfaces.each do |logical_interface| %>
        <tr id="logical_interface_<%= logical_interface.id %>">
            <td><%= logical_interface.description %></td>
            <td><%= logical_interface.subnet.network_address %></td>
            <td><%= logical_interface.bandwidth %></td>
            </td>
        </tr>
    <% end %>
</table>

logical_interface.rb

belongs_to :subnet
belongs_to :circuit

subnet.rb

belongs_to :logical_interface
belongs_to :circuit

circuit.rb

has_many :subnets
has_many :logical_interfaces

circuit_controller.rb

The CRUD is being done inside the controller of another model because this is the main object which everything else runs off.

def update

  ....
  if params[:id]
    @circuit = Circuit.find(params[:id])
  end

  @logical_interfaces = LogicalInterface.find_all_by_circuit_id(@circuit.id)
  ....

end

As is common with bugs, I'll bet this is something silly but I genuinely can't work it out for myself so would greatly appreciate any answers. I have a feeling it's to do with the relationships that have been defined between the models but I could be wrong.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
  • 1
    One of your records `logical_interface` has not subnet associated to it, so when call `logical_interface.subnet.network_address`, it raises "Nil does not have a 'network_address' method" => use `logical_interface.subnet.try(:network_address)` to see which of the record is corrupted and add validates presence on the Subnet model if subnet should be mandatory for a logical_interface. – MrYoshiji Aug 15 '13 at 21:11
  • 1
    `logical_interface.subnet` is nil, probably because that record doesn't have a subnet associated with it. You get that error because you're trying to call a method on a nil object, and the Nil object doesn't have that method. – Chris Heald Aug 15 '13 at 21:12
  • thanks, will try to dig a little further, I must have deleted something from the db.... – martincarlin87 Aug 15 '13 at 21:15

1 Answers1

1

subnet is nil for logical_interface. Either add some validation in LogicalInterface model:

validates_presence_of :subnet

or display network_address only if subnet is present.

<td><%= logical_interface.subnet.try(:network_address) %></td>
zrl3dx
  • 7,699
  • 3
  • 25
  • 35
  • thanks, this allowed me to get the page to render and see that the subnet was indeed empty but not sure how it actually got like that. – martincarlin87 Aug 15 '13 at 21:21