รับเฉพาะข้อผิดพลาด NilClass บน Heroku

ฉันได้รับข้อผิดพลาดในบันทึก Heroku ของฉัน แต่ทุกอย่างทำงานได้ดีในเครื่อง มีความคิดอะไรบ้าง?

The error: ActionView::Template::Error (undefined method `+' for nil:NilClass)

ฉันเพิ่งมีวิธีเล็ก ๆ น้อย ๆ นี้ใน user.rb ของฉัน

  def full_name
    return first_name + " " + last_name
  end

และนี่คือบันทึกของ heroku:

2013-03-05T05:34:54+00:00 app[web.1]: Started GET "/" for 98.222.28.137 at 2013-03-05 05:34:54 +0000
2013-03-05T05:34:54+00:00 heroku[router]: at=info method=GET path=/ host=objecss.herokuapp.com fwd="98.222.28.137" dyno=web.1 queue=0 wait=0ms connect=1ms service=73ms status=500 bytes=643
2013-03-05T05:34:54+00:00 app[web.1]: ActionView::Template::Error (undefined method `+' for nil:NilClass):
2013-03-05T05:34:54+00:00 app[web.1]: Processing by EntriesController#index as HTML
2013-03-05T05:34:54+00:00 app[web.1]: Completed 500 Internal Server Error in 15ms
2013-03-05T05:34:54+00:00 app[web.1]:   Rendered entries/_entries.html.erb (7.7ms)
2013-03-05T05:34:54+00:00 app[web.1]:     16:           <%=  link_to "Users", users_path %>
2013-03-05T05:34:54+00:00 app[web.1]:     12:   <% if current_user  %>
2013-03-05T05:34:54+00:00 app[web.1]:   Rendered entries/index.html.erb within layouts/application (12.5ms)
2013-03-05T05:34:54+00:00 app[web.1]:
2013-03-05T05:34:54+00:00 app[web.1]:   app/controllers/entries_controller.rb:9:in `index'
2013-03-05T05:34:54+00:00 app[web.1]:
2013-03-05T05:34:54+00:00 app[web.1]:   app/models/user.rb:21:in `full_name'
2013-03-05T05:34:54+00:00 app[web.1]:   app/views/layouts/application.html.erb:13:in `_app_views_layouts_application_html_erb___2449026742642080137_44886800'
2013-03-05T05:34:54+00:00 app[web.1]:     15:       <% if admin? %>
2013-03-05T05:34:54+00:00 app[web.1]:     13:       <%= link_to "#{current_user.full_name}", current_user %>
2013-03-05T05:34:54+00:00 app[web.1]:     11:   <%= link_to "Objecss", entries_path %>
2013-03-05T05:34:54+00:00 app[web.1]:     10: <div class="utility">
2013-03-05T05:34:54+00:00 app[web.1]:     14:       <%= link_to "Log Out", logout_path("current"), method: "delete" %>

person jthomas    schedule 05.03.2013    source แหล่งที่มา
comment
ฉันพบคำถามที่เกี่ยวข้องที่นี่: stackoverflow.com/questions/15151792/ คำตอบนั้นเกี่ยวข้องกับปัญหาของฉันหรือไม่   -  person jthomas    schedule 05.03.2013


คำตอบ (2)


สาเหตุนี้เกิดจากการไม่ได้ตั้งค่าชื่อและนามสกุลสำหรับผู้ใช้รายนี้ เพียงเปิดคอนโซล Rails บน heroku แล้วกำหนดชื่อและนามสกุลให้กับผู้ใช้นั้น คุณสามารถทำอะไรแบบนี้ได้

[56] pry(main)> u=User.find(1)
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
[58] pry(main)> u.first_name = "John"
=> "John"
[59] pry(main)> u.last_name = "Doe"
=> "Doe"
[60] pry(main)> u.save
   (0.3ms)  BEGIN
  User Exists (1.6ms)  SELECT 1 AS one FROM "users" WHERE ("users"."username" = 'example' AND "users"."id" != 1) LIMIT 1
   (1.3ms)  UPDATE "users" SET "first_name" = 'John', "last_name" = 'Doe', "updated_at" = '2013-03-05 06:01:55.263347' WHERE "users"."id" = 1
   (24.5ms)  COMMIT
=> true
person benchwarmer    schedule 05.03.2013
comment
นั่นก็คือมัน ชื่อและนามสกุลว่างเปล่า ขอบคุณ! - person jthomas; 05.03.2013

ActionView::Template::Error (วิธีการที่ไม่ได้กำหนด `+' สำหรับ nil:NilClass)

นี่เป็นการบอกคุณว่าค่าส่งคืนของ first_name คือ nil; ไม่มีวิธี + ที่กำหนดไว้ใน NilClass ดังนั้นจึงเกิดข้อผิดพลาด

พูดง่ายๆ ก็คือ อินสแตนซ์ของ User ที่คุณใช้งานบน Heroku นั้นไม่มีการตั้งค่า first_name

person deefour    schedule 05.03.2013