nil的未定义方法:Heroku上的NilClass,但在本地工作

时间:2014-03-13 04:32:54

标签: ruby-on-rails heroku null

我在RoR很新,但是我的项目无法在Heroku上工作,虽然它在本地工作得很好。问题似乎是当我尝试使用recruiter.headhunter.CompanyName行时,我得到了一个ActionView :: Template :: Error(未定义的方法`CompanyName' for nil:NilClass)。

我尝试搜索过去几天(似乎是几个不同原因的常见问题)无济于事。我尝试过迁移并重新启动Heroku但我仍然遇到错误。非常感谢任何帮助,谢谢你的时间。

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日志

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>

1 个答案:

答案 0 :(得分:1)

这个错误是由于一些招聘人员记录猎头失踪。请尝试以下代码。

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