【问题标题】:Rails undefined method error (ONLY in development)Rails 未定义的方法错误(仅在开发中)
【发布时间】:2013-09-19 05:04:07
【问题描述】:

我得到的错误是在我的部分 _pins.html.erb 中:

引脚中的 NoMethodError#index -- nil:NilClass 的未定义方法 `avatar' 提取的源代码(第 4 行附近):

3:      <div class="avatar-position">
4:          <%= image_tag pin.user.avatar.url(:thumb), class: "thumbsize" %>
5:      </div>

pins/index 视图 (index.html.erb) 有 &lt;%= render @pins %&gt;

我通过索引控制器渲染它,所以在 pins_controller.rb 我有:

  def index
    @pins = Pin.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @pins }
    end
  end

奇怪的是它在生产中运行良好。当我推送到 heroku 时,我没有收到任何错误,一切都很好。我最近确实将我的 Mac OSX 更新到 10.8.5,但如果那是罪魁祸首,我不是吗?不同的是,在开发中,我的图像资产(头像)保存在本地,而在生产中,它们使用回形针保存在亚马逊 AWS 上......有什么想法吗?

【问题讨论】:

  • 问题是第 4 行中没有与 pin 关联的用户记录。
  • 这里的局部变量pin是什么?在&lt;%= image_tag pin.user.avatar.url(:thumb), class: "thumbsize" %&gt; 和一些pin 没有用户所以你得到avatar' for nil:NilClass 因为pin.usernil
  • 我尝试重新启动本地服务器,然后重新启动我的计算机,没有运气。
  • 每个用户都有很多引脚 - 我基本上是试图通过用户的引脚 ID 识别用户来获取存储的用户头像

标签: ruby-on-rails paperclip development-environment production-environment nomethoderror


【解决方案1】:

这里的局部变量pin 是什么?在&lt;%= image_tag pin.user.avatar.url(:thumb), class: "thumbsize" %&gt; 和一些pin has no user 所以你得到avatar' for nil:NilClass 因为pin.usernil

就这样吧

<%= image_tag pin.user.avatar.url(:thumb), class: "thumbsize"  unless pin.user.nil? %>

【讨论】:

  • 你是摇滚明星,成功了!我想问题是在我的开发数据库中,有些用户没有头像......这解释了为什么它在所有头像都在那里的生产环境中工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多