Newbie Question - Nil Error in ActiveController

Hi everyone, looking for some help with this error below. It seems
that althought it finds the product_line_id attribute, the code
doesn’t find the product_line’s name attribute. I’ve already declared
the has_many: call_driver relationship in the product_line model, and
the belongs_to: product_line relationship in the call_driver model.

It also works in the console, oddly enough, but not in the view.

I know I’m missing something elemental here, but I haven’t the
faintest clue what it is.

NoMethodError in Admin#index

Showing admin/index.html.erb where line #13 raised:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name
Extracted source (around line #13):

10: <% for driver in @driver %>
11:


12: <%=h driver.name %>
13: <%=h driver.product_line.name %>
14: <%= link_to ‘Edit’, {:action => ‘driver_edit’} %>
15: <%= link_to ‘Destroy’, {:action => ‘driver_delete’,:id =>
driver.id} %>
16:

admin_controller has this for the index action;

def index
@driver = CallDriver.find(:all)

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @driver }
end

end

What’s interesting is that I’ve done almost entirely the same thing
with another view, and it works;

View

<% for driver in @drivers %>

<%= driver.name %> <%= driver.product_line.name %> <% end %>
Controller

class ShowController < ApplicationController
def show_drivers

Find Call Drivers

@drivers =
CallDriver.find(:all, :conditions => [
‘product_line_id = :product_line and
sub_product_line_id = :sub_product_line and
platform_id = :platform and
consolidation_id = :consolidation and
segment_id = :segment’,
{:product_line => params[:product_line],
:sub_product_line => params[:sub_product_line],
:platform => params[:platform],
:consolidation => params[:consolidation],
:segment => params[:segment]}])
respond_to do |format|
format.html
format.xml { render :xml => @drivers }
end
end
end

Please disregard, I’ve discovered the problem. I’m an idiot and forgot
that some of my data didn’t have a product_line_id set.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs