Heroku 500内部服务器错误nil:NilClass的未定义方法'username' [英] Heroku 500 internal server error undefined method 'username' for nil:NilClass
问题描述
我的应用程序可以在我的本地服务器上正常运行,但是当我将它从git推送到heroku时,它无法在heroku上运行.我已经运行heroku运行rake db:migrate,但是仍然没有运气.我猜想它与用户名方法有关,但不确定为什么它可以在我的本地而不是在heroku上工作.
My app is able to run on my local server just fine, but when I pushed it from git to heroku it's not working on heroku. I've run heroku run rake db:migrate, but still no luck. I'm guessing it has something to do with the username method, but not sure why it would work on my local and not on heroku.
2013-03-01T05:45:00+00:00 app[web.1]:
2013-03-01T05:45:00+00:00 app[web.1]: 11: <% if current_user == pin.user %>
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: <strong>
2013-03-01T05:45:01+00:00 app[web.1]: 8: Posted by <%= pin.user.username %>
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: </p>
2013-03-01T05:45:01+00:00 app[web.1]: 6: <p>
2013-03-01T05:45:01+00:00 app[web.1]: 9: </strong>
2013-03-01T05:45:01+00:00 app[web.1]: 11: <% if current_user == pin.user %>
2013-03-01T05:45:01+00:00 app[web.1]: 10: </p>
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: <p>
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: <% if current_user == pin.user %>
2013-03-01T05:45:02+00:00 app[web.1]: 8: Posted by <%= pin.user.username %>
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: </p>
2013-03-01T05:45:02+00:00 app[web.1]: 5: </p>
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: <strong>
2013-03-01T05:45:02+00:00 app[web.1]: cache: [GET /] miss
2013-03-01T05:45:02+00:00 app[web.1]: 9: </strong>
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 <%= pin.user.username %>
2013-03-01T05:45:02+00:00 app[web.1]: 5: </p>
2013-03-01T05:45:02+00:00 app[web.1]: 6: <p>
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: <strong>
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: <% if current_user == pin.user %>
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: </strong>
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: </p>
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: </p>
2013-03-01T05:45:02+00:00 app[web.1]: 6: <p>
2013-03-01T05:45:02+00:00 app[web.1]: 8: Posted by <%= pin.user.username %>
2013-03-01T05:45:02+00:00 app[web.1]: 9: </strong>
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: <% if current_user == pin.user %>
2013-03-01T05:45:02+00:00 app[web.1]: 10: </p>
2013-03-01T05:45:02+00:00 app[web.1]: 7: <strong>
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下的代码:
Here is the code under 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)引起的.保存密码时,请确保已正确分配用户.
Your username method is fine - the problem is caused by your pin instance not having a user (i.e. pin.user is nil). When saving the pin, make sure a user is correctly assigned.
这篇关于Heroku 500内部服务器错误nil:NilClass的未定义方法'username'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!