0

我正在建立一个基本博客,并为帖子提供可选的图片上传。图像正在正确上传并进入正确的目录。但是,当我转到视图时,它会加载默认图像:

photos/original/missing.png

这是模型

class Post < ActiveRecord::Base
  attr_accessible :body, :date, :feature, :poster, :title, :photo

  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
                    :url  => "/assets/posts/:id/:style/:basename.:extension",
                    :path => ":rails_root/public/assets/posts/:id/:style/:basename.:extension"

  attr_accessor :photo_file_name
  attr_accessor :photo_content_type
  attr_accessor :photo_file_size
  attr_accessor :photo_updated_at
end

在视图中:

<%= image_tag @post.photo.url %>

例如,我上传了一张带有帖子的图片,它被上传到:

rails_root/public/assets/posts/5/original/image.jpg
rails_root/public/assets/posts/5/medium/image.jpg
rails_root/public/assets/posts/5/thumb/image.jpg

移民

class AddAttachmentImageToPosts < ActiveRecord::Migration
  def self.up
    add_attachment :posts, :photo
  end

  def self.down
    remove_attachment :posts, :photo
  end
end

架构:

  create_table "posts", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "date"
    t.string   "poster"
    t.boolean  "feature"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

然而,当视图被渲染时,它找不到图像。我在这里想念什么?

4

1 回答 1

1

尝试 attr_accessible 而不是 attr_accessor 用于照片列。

所以

class Post < ActiveRecord::Base

  attr_accessible :body, :date, :feature, :poster, :title, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at

  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
                :url  => "/assets/posts/:id/:style/:basename.:extension",
                :path => ":rails_root/public/assets/posts/:id/:style/:basename.:extension"
end

更新后编辑:

您的数据库和回形针设置不匹配。将所有列更改为 photo_x 或更改将照片设置为图像的设置。

于 2013-04-01T00:05:36.900 回答