未定义的方法`image'为#

时间:2014-10-02 17:50:43

标签: ruby-on-rails

我安装了gem'paperclip'并将其用作railscast中的show.Image出现在页面“show”中,但在页面“index”中显示错误:

undefined method `image' for #<Article::ActiveRecord_Relation:0x007fbab4814310>

这是我的index.html.erb

<% @article.each do |article| %>
      <div class="main-page-blocks">
      <%= image_tag @article.image.url(:small) %>
      <h3><%= article.title %></h3>
      <div class="main-page-articles"><%= article.body %></div>
      </div>
  <% end %>

这是我的模特文章.rb

class Article < ActiveRecord::Base
    has_attached_file :image, :styles => { :small => "150x150"}
    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end

哪里错了?

1 个答案:

答案 0 :(得分:3)

以下是问题所在:

<%= image_tag @article.image.url(:small) %>

您在image上调用@article时,它应该是article,这是each块中定义的实例变量。

更新如下:

<% @article.each do |article| %>
    <div class="main-page-blocks">
      <%= image_tag article.image.url(:small) %>
      <h3><%= article.title %></h3>
      <div class="main-page-articles"><%= article.body %></div>
    </div>
<% end %>