我正在建立一个基本博客,并为帖子提供可选的图片上传。图像正在正确上传并进入正确的目录。但是,当我转到视图时,它会加载默认图像:
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
然而,当视图被渲染时,它找不到图像。我在这里想念什么?