0

我到处搜索,根本没有找到解决方案。这是我的问题:

在我的“视图”文件夹中,我有“图钉”和“页面”文件夹。

在“views/pins/”中,我创建了一个名为“_pin.html.erb”的文件,在里面我写了这个:

<%= image_tag pin.image(:medium) %>

现在在我的“views/pages”文件夹中,我有一个名为“home.html.erb”的文件,在其中,我尝试使用这个来渲染“_pin.html.erb”的代码:

<%= render "pins/pin" %>

但我收到此错误:

undefined method `image' for nil:NilClass 
Extracted source (around line #1):
<%= image_tag pin.image(:medium) %>

那么,可能是什么问题?我正在使用回形针,并尝试使用以下方法在“views/pins/index.html.erb”中呈现相同的代码:

<%= render @pins %>

它呈现完美,但在“views/pages/home.html.erb”文件(如上所述)中,我收到错误!

任何人都可以帮助我吗?

在我的“models/pin.rb”文件中,我有这个:

class Pin < ActiveRecord::Base
  attr_accessible :description, :image

  validates :description, presence: true

  belongs_to :user
  has_attached_file :image
  validates :user_id, presence: true
  validates_attachment :image, presence: true,
                        content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif']},
                        size: { less_than: 5.megabytes}
  has_attached_file :image, styles: { medium: "320x240"}
end

我尽力解释我的问题,请告诉我我做错了什么?谢谢你。

4

1 回答 1

2

Railspin在使用render @pins. 如果要使用部分,则必须提供要使用的集合,如下所示:

<%= render partial: 'pins/pin', collection: @pins %>
于 2013-10-08T13:03:10.077 回答