【问题标题】:NoMethodError when implementing Instagram Gem for Ruby on Rails website为 Ruby on Rails 网站实施 Instagram Gem 时出现 NoMethodError
【发布时间】:2014-12-24 07:59:01
【问题描述】:

我刚刚完成了一个月的 Rails 教程,这是我建立的网站:https://warwicktri.herokuapp.com/。现在我正在尝试将 Instagram 提要拉到主页上,但在我的本地主机上的 Pins#index 中出现 NoMethodError。

undefined method `each' for nil:NilClass

<div>
  <% @instagram.each do |instagram| %>
    <%= image_tag instagram.images.first.last.url %>
  <% end %>
</div>

我认为这意味着@instagram 没有正确声明,但我不知道我哪里出错了。

这是我的 pins_controller.rb 中的代码:

def index
  @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
  respond_with(@pins)
  @instagram = Instagram.user_recent_media("264208635")
  respond_with(@instagram)
end

我也尝试将@instagram 单独放在另一个控制器中,但我仍然得到同样的错误:

class InstagramController < ApplicationController
  def index
    @instagram = Instagram.user_recent_media("264208635")
    respond_with(@instagram)
  end
end

我应该如何在我的代码中声明@instagram?

更新 1:我的视图代码 (index.html.erb):

<div id="pins" class="transitions-enabled">
  <% @pins.each do |pin| %>
    <div class="box panel panel-default">
      <%= link_to image_tag(pin.image.url(:medium)), pin %>
      <div class="panel-body">
        <p><%= pin.description %></p>
        <p><strong><%= pin.user.name if pin.user %></strong></p>
        <% if pin.user == current_user %>
          <div class="actions">
            <%= link_to edit_pin_path(pin) do %>
              <span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Edit
            <% end %>
            <%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
              <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete
            <% end %>
          </div>
        <% end %>  
      </div>
    </div>
  <% end %>
</div>

<div class="center">
  <%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
</div>

<div>
    <% @instagram.each do |instagram| %>
      <%= image_tag instagram.images.first.last.url %>
    <% end %>
</div>

【问题讨论】:

  • 看来您正在渲染不属于该操作的视图,在您的情况下是index
  • 你有什么看法?
  • 在rails控制台中运行Instagram.user_recent_media("264208635")可能是返回nil
  • @МалъСкрылевъ,我已经更新了我的视图的代码 (index.html.erb)。你介意解释你的第一个陈述吗?抱歉,我一个月前才开始使用 Rails,所以我还是很业余。
  • @RajarshiDas,我已经完成了,我收到了与用户的 Instagram 帐户相关的整个代码块。我看到了帖子的标题、用户名、过滤器信息等。

标签: ruby-on-rails instagram instagram-api


【解决方案1】:

在朋友的帮助下,我意识到自己哪里出错了,以及如何纠正 NoMethodError。

这个答案有两个部分:
1) 将 Instagram 代码添加到引脚控制器
2) 对 Instagram 数据使用单独的控制器

请注意,此答案专门针对 One Month Rails 教程,可能直接适用于每个 Web 应用程序,也可能不适用。

1) 将 Instagram 代码添加到引脚控制器中

  • Mattan Griffel 关于respond_with(@pins) 部分是正确的。 respond_with(@pins) 导致我的控制器操作跳过其余部分 那个部分。因此,当我更改代码的顺序时,它起作用了。
  • 当我在 Mattan 建议之后尝试删除它时,我很可能又犯了另一个错误,所以它当时不起作用(参见上面的 cmets)。

我的pins_controller.rb的一部分:

  def index
    @instagram = Instagram.user_recent_media("264208635")
    @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
    respond_with(@pins)
  end

2) 对 Instagram 数据使用单独的控制器

  • 即使我使用单独的控制器,我也会得到相同的 NoMethodError,因为当我在我的 instagram_controller.rb 中声明 @instagram 时,我试图将数据渲染到我的 pin 视图 (views/pins/index.html.erb) .但是,我没有在我的 pins_controller.rb 中声明 @instagram
  • 于是我从instagram_controller.rb中复制了@instagram = Instagram.user_recent_media("264208635"),粘贴到我的pins_controller.rb下index方法下;像这样:

我的pins_controller.rb的一部分:

  def index
    @instagram = Instagram.user_recent_media("264208635")
    @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 5)
    respond_with(@pins)
  end

同样,respond_with(@pins) 会导致我的控制器操作跳过其余部分,因此必须在 respond_with(@pins) 之前而不是之后添加 @instagram = Instagram.user_recent_media("264208635")。然后,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    相关资源
    最近更新 更多