nil的未定义方法'link':NilClass

时间:2012-12-14 12:48:07

标签: ruby-on-rails

我已将我的项目上传到heroku,但我收到此错误:NoMethodError in Items#hype_itemundefined method "link" for nil:NilClass。当我使用localhost时,它可以在我的计算机上运行。

我做到了这一点:

  1. heroku运行rake db:create
  2. herouk run rake db:migrate
  3. heroku restart
  4. ...并阅读了很多关于这个问题的帖子,但它没有解决它。

    有什么建议吗?

    这是日志

    2012-12-14T11:54:02+00:00 app[web.1]: Processing by ItemsController#hype_item as HTML
    2012-12-14T11:54:03+00:00 app[web.1]:   Rendered items/hype_item.html.erb within layouts/application (75.2ms)
    2012-12-14T11:54:03+00:00 app[web.1]: Completed 500 Internal Server Error in 405ms
    2012-12-14T11:54:03+00:00 app[web.1]: 
    2012-12-14T11:54:03+00:00 app[web.1]: ActionView::Template::Error (undefined method `link' for nil:NilClass):
    2012-12-14T11:54:03+00:00 app[web.1]:     6:        <p><div id="abc"><%= raw @item.link %></div></p>
    2012-12-14T11:54:03+00:00 app[web.1]:     8: </div>
    2012-12-14T11:54:03+00:00 app[web.1]:     3: 
    2012-12-14T11:54:03+00:00 app[web.1]:   app/views/items/hype_item.html.erb:6:in `_app_views_items_hype_item_html_erb___686068816707788662_29579640'
    2012-12-14T11:54:03+00:00 app[web.1]: 
    2012-12-14T11:54:03+00:00 app[web.1]:     4: <div class="modal hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="tr">
    2012-12-14T11:54:03+00:00 app[web.1]:     5:    <div class="modal-body">
    2012-12-14T11:54:03+00:00 app[web.1]:     7:    </div>
    2012-12-14T11:54:03+00:00 app[web.1]: 
    2012-12-14T11:54:03+00:00 app[web.1]:   Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (18.7ms)
    2012-12-14T11:54:03+00:00 heroku[router]: at=info method=GET path=/ host=enigmatic-tor-2280.herokuapp.com fwd=84.48.62.139 dyno=web.1 queue=0 wait=0ms connect=16ms service=773ms status=500 bytes=26039
    2012-12-14T11:54:03+00:00 app[web.1]:   Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (29.9ms)
    2012-12-14T11:54:03+00:00 app[web.1]:   Rendered vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
    

    ...和gemfile

    source 'https://rubygems.org'
    
    gem 'rails', '3.2.6'
    
    # Bundle edge Rails instead:
    # gem 'rails', :git => 'git://github.com/rails/rails.git'
    gem "less-rails", "~> 2.2.6"
    gem 'therubyracer'
    gem 'bcrypt-ruby', :require => 'bcrypt'
    
    
    group :assets do
      gem 'sass-rails',   '~> 3.2.3' #mulig denne må flyttes ut av asset group for å virke
      gem 'coffee-rails', '~> 3.2.1'
    
      # See https://github.com/sstephenson/execjs#readme for more supported runtimes
      # gem 'therubyracer', :platforms => :ruby
    
      gem 'uglifier', '>= 1.0.3'
      gem 'twitter-bootstrap-rails'
    end
    
    group :production do
      gem 'pg'
    end
    group :development, :test do
      gem 'sqlite3'
    end
    
    gem 'thin'
    gem 'jquery-rails'
    

    hype_item.html.erb

    <div id="counter"></div>
    <div id="play_button"> <a id="play" role="button"><%= image_tag("Play.png")%></a></div>
    
    <div class="modal hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="tr">
        <div class="modal-body">
            <p><div id="abc"><%= raw @item.link %></div></p>
        </div>
    </div>
    

    告诉我你是否需要查看更多文件,我不确定是否相关。

1 个答案:

答案 0 :(得分:0)

正如Baldrick所指出的那样,问题是@item是零。

所以我改变了(在Hype_item.html.erb中):

<p><div id="abc"><%= raw @item.link %></div></p>

为:

<p><div id="abc"><% if @item %> <%= raw @item.link %> <% end %></div></p>

这可能不是最佳解决方案,但它有效。

随意提供更好的解决方案,也许这可以在控制器中完成。

感谢所有反馈!