Heroku 500内部服务器错误未定义方法’用户名’为nil:NilClass

我的应用程序能够在我的本地服务器上运行得很好,但当我将它从git推送到heroku时,它不能在heroku上运行。 我运行heroku运行rake db:migrate,但仍然没有运气。 我猜它与用户名方法有关,但不确定为什么它可以在我的本地而不是在heroku上工作。

2013-03-01T05:45:00+00:00 app[web.1]: 2013-03-01T05:45:00+00:00 app[web.1]: 11:  2013-03-01T05:45:00+00:00 app[web.1]: app/views/pins/index.html.erb:4:in `_app_views_pins_index_html_erb__3245926179851309408_37549160' 2013-03-01T05:45:00+00:00 app[web.1]: cache: [GET /] miss 2013-03-01T05:45:00+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: Started GET "/" for 99.190.128.33 at 2013-03-01 05:45:01 +0000 2013-03-01T05:45:01+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: Processing by PinsController#index as HTML 2013-03-01T05:45:01+00:00 heroku[router]: at=info method=GET path=/ host=railsapp- test.herokuapp.com fwd="99.190.128.33" dyno=web.1 queue=0 wait=0ms connect=0ms service=38ms status=500 bytes =643 2013-03-01T05:45:01+00:00 app[web.1]: Rendered static_pages/_home.html.erb (3.7ms) 2013-03-01T05:45:01+00:00 app[web.1]: Rendered pins/_pin.html.erb (11.3ms) 2013-03-01T05:45:01+00:00 app[web.1]: Rendered pins/index.html.erb within layouts/application (20.5ms) 2013-03-01T05:45:01+00:00 app[web.1]: 7:  2013-03-01T05:45:01+00:00 app[web.1]: 8: Posted by  2013-03-01T05:45:01+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: app/views/pins/_pin.html.erb:8:in `_app_views_pins__pin_html_erb___1817132116748238264_25273940' 2013-03-01T05:45:01+00:00 app[web.1]: app/views/pins/index.html.erb:4:in `_app_views_pins_index_html_erb__3245926179851309408_37549160' 2013-03-01T05:45:01+00:00 app[web.1]: app/controllers/pins_controller.rb:8:in `index' 2013-03-01T05:45:01+00:00 app[web.1]: Completed 500 Internal Server Error in 22ms 2013-03-01T05:45:01+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: ActionView::Template::Error (undefined method `username' for nil:NilClass): 2013-03-01T05:45:01+00:00 app[web.1]: 5: 

2013-03-01T05:45:01+00:00 app[web.1]: 6:

2013-03-01T05:45:01+00:00 app[web.1]: 9: 2013-03-01T05:45:01+00:00 app[web.1]: 11: 2013-03-01T05:45:01+00:00 app[web.1]: 10:

2013-03-01T05:45:01+00:00 app[web.1]: 2013-03-01T05:45:01+00:00 app[web.1]: cache: [GET /] miss 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: Started GET "/" for 99.190.128.33 at 2013-03-01 05:45:02 +0000 2013-03-01T05:45:02+00:00 app[web.1]: Processing by PinsController#index as HTML 2013-03-01T05:45:02+00:00 app[web.1]: Rendered static_pages/_home.html.erb (4.4ms) 2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/index.html.erb within layouts/application (22.2ms) 2013-03-01T05:45:02+00:00 app[web.1]: 6:

2013-03-01T05:45:02+00:00 app[web.1]: Completed 500 Internal Server Error in 23ms 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: ActionView::Template::Error (undefined method `username' for nil:NilClass): 2013-03-01T05:45:02+00:00 app[we b.1]: app/views/pins/_pin.html.erb:8:in `_app_views_pins__pin_html_erb___1817132116748238264_25273940' 2013-03-01T05:45:02+00:00 app[web.1]: app/controllers/pins_controller.rb:8:in `index' 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: 11: 2013-03-01T05:45:02+00:00 app[web.1]: 8: Posted by 2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/_pin.html.erb (12.4ms) 2013-03-01T05:45:02+00:00 app[web.1]: 10:

2013-03-01T05:45:02+00:00 app[web.1]: 5:

2013-03-01T05:45:02+00:00 app[web.1]: app/views/pins/index.html.erb:4:in `_app_views_pins_index_html_erb__3245926179851309408_37549160' 2013-03-01T05:45:02+00:00 app[web.1]: 7: 2013-03-01T05:45:02+00:00 app[web.1]: cache: [GET /] miss 2013-03-01T05:45:02+00:00 app[web.1]: 9: 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: Started GET "/" for 99.190.128.33 at 2013-03-01 05:45:02 +0000 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: Processing by PinsController#index as HTML 2013-03-01T05:45:02+00:00 app[web.1]: Rendered static_pages/_home.html.erb (1.6ms) 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 heroku[router]: at=info method=GET path=/ host=railsapp- test.herokuapp.com fwd="99.190.128.33" dyno=web.1 queue=0 wait=3ms connect=2ms service=63ms status=500 bytes =643 2013-03-01T05:45:02+00:00 app[web.1]: 8: Posted by 2013-03-01T05:45:02+00:00 app[web.1]: 5:

2013-03-01T05:45:02+00:00 app[web.1]: 6:

2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/_pin.html.erb (47.0ms) 2013-03-01T05:45:02+00:00 app[web.1]: 7: 2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/index.html.erb within layouts/application (50.3ms) 2013-03-01T05:45:02+00:00 app[web.1]: 11: 2013-03-01T05:45:02+00:00 app[web.1]: app/views/pins/_pin.html.erb:8:in `_app_views_pins__pin_html_erb___1817132116748238264_25273940' 2013-03-01T05:45:02+00:00 app[web.1]: app/controllers/pins_controller.rb:8:in `index' 2013-03-01T05:45:02+00:00 app[web.1]: 9: 2013-03-01T05:45:02+00:00 app[web.1]: app/views/pins/index.html.erb:4:in `_app_views_pins_index_html_erb__3245926179851309408_37549160' 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: cache: [GET /] miss 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: 10:

2013-03-01T05:45:02+00:00 app[web.1]: Completed 500 Internal Server Error in 51ms 2013-03-01T05:45:02+00:00 app[web.1]: ActionView::Template::Error (undefined method `username' for nil:NilClass): 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: Started GET "/" for 99.190.128.33 at 2013-03-01 05:45:02 +0000 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: Processing by PinsController#index as HTML 2013-03-01T05:45:02+00:00 app[web.1]: Rendered static_pages/_home.html.erb (2.8ms) 2013-03-01T05:45:02+00:00 app[web.1]: Completed 500 Internal Server Error in 14ms 2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/index.html.erb within layouts/application (12.7ms) 2013-03-01T05:45:02+00:00 app[web.1]: Rendered pins/_pin.html.erb (8.0ms) 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: ActionView::Template::Error (undefined method `username' for nil:NilClass): 2013-03-01T05:45:02+00:00 app[web.1]: 5:

2013-03-01T05:45:02+00:00 app[web.1]: 6:

2013-03-01T05:45:02+00:00 app[web.1]: 8: Posted by 2013-03-01T05:45:02+00:00 app[web.1]: 9: 2013-03-01T05:45:02+00:00 app[web.1]: app/views/pins/_pin.html.erb:8:in `_app_views_pins__pin_html_erb___1817132116748238264_25273940' 2013-03-01T05:45:02+00:00 app[web.1]: 11: 2013-03-01T05:45:02+00:00 app[web.1]: 10:

2013-03-01T05:45:02+00:00 app[web.1]: 7: 2013-03-01T05:45:02+00:00 heroku[router]: at=info method=GET path=/ host=railsapp- test.herokuapp.com fwd="99.190.128.33" dyno=web.1 queue=0 wait=0ms connect=3ms service=22ms status=500 bytes =643 2013-03-01T05:45:02+00:00 app[web.1]: app/views/pins/index.html.erb:4:in `_app_views_pins_index_html_erb__3245926179851309408_37549160' 2013-03-01T05:45:02+00:00 app[web.1]: app/controllers/pins_controller.rb:8:in `index' 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: 2013-03-01T05:45:02+00:00 app[web.1]: cache: [GET /] miss 2013-03-01T05:45:02+00:00 heroku[router]: at=info method=GET path=/ host=railsapp-test.herokuapp.com fwd="99.190.128.33" dyno=web.1 queue=0 wait=0ms connect=1ms service=32ms status=500 bytes =643

编辑:这是pins_controller.rb下的代码:

 def create @pin = current_user.pins.new(params[:pin]) respond_to do |format| if @pin.save format.html { redirect_to pins_path, notice: 'Pin was successfully created.' } format.json { render json: @pin, status: :created, location: @pin } else format.html { render action: "new" } format.json { render json: @pin.errors, status: :unprocessable_entity } end end end 

您的用户名方法很好 – 问题是由您的pin实例没有用户引起的(即pin.user为nil)。 保存引脚时,请确保正确分配了用户。