1
我比較新的RoR,並且無法讓我的項目在Heroku上工作,雖然它在本地工作的很好。這個問題似乎是,當我嘗試使用recruiter.headhunter.CompanyName行時,我得到了一個ActionView :: Template :: Error(未定義的方法`CompanyName'爲nil:NilClass)。nil的未定義方法:Heroku上的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>
這完全解決了我的問題。非常感謝! – Philnancials