3
undefined method `url' for #<GalleryPhoto:0x007f80c05a4ba8>

10:   <%= @gallery.date %>
11: </p>
12: 
13: <%= @gallery.gallery_photos.first.url %>
14: 
15: 
16: <%= link_to 'Edit', edit_gallery_path(@gallery) %>

我正在尝试在 Rails 应用程序中创建相册系统,在该应用程序中创建相册并通过回形针将图像上传到其中。我无法让 .url 方法在我的显示页面上工作以显示图像。它的设置方式是这样的:

画廊模型(有许多画廊照片)

GalleryPhotos 模型(belongs_to gallery)

画廊展示:

<p id="notice"><%= notice %></p>

<p>
  <b>Gallery name:</b>
  <%= @gallery.gallery_name %>
</p>

<p>
  <b>Date:</b>
  <%= @gallery.date %>
</p>

<%= @gallery.gallery_photos.first.url %>


<%= link_to 'Edit', edit_gallery_path(@gallery) %> |
<%= link_to 'Back', galleries_path %>

画廊模型

class Gallery < ActiveRecord::Base
    attr_accessible :date, :gallery_name, :gallery_photos_attributes
    has_many :gallery_photos, :dependent => :destroy

    accepts_nested_attributes_for :gallery_photos

end

画廊照片模型

class GalleryPhoto < ActiveRecord::Base
    attr_accessible :photo, :caption, :date, :gallery_id

    belongs_to :gallery

    has_attached_file :photo,:styles => { :large => "300x300<", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

end

画廊控制器

  def new
    @gallery = Gallery.new
    @gallery.gallery_photos.build # added this
  end

  def show
    @gallery = Gallery.find(params[:id])
  end

  def create
    @gallery = Gallery.new(params[:gallery])

    respond_to do |format|
      if @gallery.save!
        format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
        format.json { render json: @gallery, status: :created, location: @gallery }
      else
        format.html { render action: "new" }
        format.json { render json: @gallery.errors, status: :unprocessable_entity }
      end
    end
  end

该表是 mysql,我正在通过一个 vagrant 虚拟系统运行它。它正在插入新的,并且正在制作中。在新版本中,它将数据插入到画廊和画廊照片的表中。无论我做什么,我都无法从中获取网址。

4

1 回答 1

9

来自https://github.com/thoughtbot/paperclip,该url方法属于
has_attached_file :photo,因此检索 url 的正确方法是

@gallery.gallery_photos.first.photo.url
于 2013-05-03T15:34:24.667 回答