Philnancials
Philnancials

Reputation: 13

Undefined method for nil:NilClass on Heroku but works locally

I'm rather new at RoR and am having trouble getting my project to work on Heroku, although it works locally just fine. The issue appears to be that I'm getting a ActionView::Template::Error (undefined method `CompanyName' for nil:NilClass) when I'm trying to use the recruiter.headhunter.CompanyName line.

I've tried searching for the past few days (seems to be a common problem with a few different causes) to no avail. I've tried migrating and restarting Heroku but I still get the error. Any help would be greatly appreciated, thank you for your time.

index.html.erb

<tbody>
  <% @recruiters.each do |recruiter| %>
    <tr>
      <td><%= link_to recruiter.name, recruiter %></td>
      <td><%= recruiter.headhunter.CompanyName %></td>
    </tr>
  <% end %>
</tbody>

recruiters_controller.rb

def index
    Recruiter.joins(:recruiter, :headhunter).where("recruiter.headhunter_id = headhunter.id")
    @recruiters = Recruiter.all.order("updated_at DESC")
  end

recruiter.rb

class Recruiter < ActiveRecord::Base
    belongs_to :headhunter
end

headhunter.rb

class Headhunter < ActiveRecord::Base
    has_many :recruiters
end

schema.rb

create_table "headhunters", force: true do |t|
    t.string   "CompanyName"
  end

  create_table "recruiters", force: true do |t|
    t.string   "name"
    t.integer  "headhunter_id"
  end

  add_index "recruiters", ["headhunter_id"], name: "index_recruiters_on_headhunter_id"

heroku log

2014-03-13T04:02:45.827745+00:00 app[web.1]:     17:         <td>
2014-03-13T04:02:45.827745+00:00 app[web.1]: 
2014-03-13T04:02:45.827745+00:00 app[web.1]:     15:         <td><%= link_to recruiter.name, recruiter %></td>
2014-03-13T04:02:45.827745+00:00 app[web.1]:     16:         <td><%= recruiter.headhunter.CompanyName %></td>
2014-03-13T04:02:45.825779+00:00 app[web.1]: Completed 500 Internal Server Error in 518ms
2014-03-13T04:02:45.827745+00:00 app[web.1]:     19:             <span class="glyphicon glyphicon-edit"></span>
2014-03-13T04:02:45.827745+00:00 app[web.1]:   app/views/recruiters/index.html.erb:16:in `block in _app_views_recruiters_index_html_erb___3641275317349014078_70093617038520'
2014-03-13T04:02:45.825779+00:00 app[web.1]: Completed 500 Internal Server Error in 518ms
2014-03-13T04:02:45.827745+00:00 app[web.1]:     14:       <tr>
2014-03-13T04:02:45.827745+00:00 app[web.1]:     13:     <% @recruiters.each do |recruiter| %>
2014-03-13T04:02:45.827963+00:00 app[web.1]: 
2014-03-13T04:02:45.827963+00:00 app[web.1]: ActionView::Template::Error (undefined method `CompanyName' for nil:NilClass):
2014-03-13T04:02:45.827963+00:00 app[web.1]:   app/views/recruiters/index.html.erb:13:in `_app_views_recruiters_index_html_erb___3641275317349014078_70093617038520'
2014-03-13T04:02:45.827963+00:00 app[web.1]: 
2014-03-13T04:02:45.827963+00:00 app[web.1]:     13:     <% @recruiters.each do |recruiter| %>
2014-03-13T04:02:45.827963+00:00 app[web.1]:     15:         <td><%= link_to recruiter.name, recruiter %></td>
2014-03-13T04:02:45.827745+00:00 app[web.1]:     18:           <%= link_to edit_recruiter_path(recruiter) do %>
2014-03-13T04:02:45.827963+00:00 app[web.1]:     14:       <tr>
2014-03-13T04:02:45.827963+00:00 app[web.1]: 
2014-03-13T04:02:45.828145+00:00 app[web.1]:     19:             <span class="glyphicon glyphicon-edit"></span>
2014-03-13T04:02:45.827963+00:00 app[web.1]:     17:         <td>
2014-03-13T04:02:45.827963+00:00 app[web.1]:     16:         <td><%= recruiter.headhunter.CompanyName %></td>

Upvotes: 1

Views: 798

Answers (1)

Bachan Smruty
Bachan Smruty

Reputation: 5734

This error is due to for some recruiter records headhunter is missing. Try the following code.

<td><%= recruiter.headhunter.try(:CompanyName).blank? ? 'N/A' : recruiter.headhunter.CompanyName %></td>

Upvotes: 1

Related Questions