2012-11-04 23 views
1

回形针能够成功上传图像(将其上传到服务器上的相应文件夹)。但是,当我尝试查看图像时,它会将其报告为丢失。回形针报告上传的图像丢失。我怎样才能找到它?

型号/ photo.rb:

class Photo < ActiveRecord::Base 
attr_accessible :image 
attr_accessor :image_file_name, :image_file_size, :image_updated_at 
has_attached_file :image, styles: {small: "300x300>" }, 
url "/photos/:id/:style/:basename.:extension", 
path: ":rails_root/public/photos/:id/:style/:basename.:extension" 


validates_attachment_presence :image 
validates_attachment_size :image, less_than: 5.megabytes 
end 

观看图像(照片/ show.html.erb):

<% if @photo.image? %> 
    <%= image_tag @photo.image.url(:small) %> 
<% else %> 
    Missing image <%= @photo.id %> 
<% end %> 

这个输出 “缺少像” 用正确的带照片的身份证。

尽管如此,上传的照片存储在正确的路径,并可通过正确的URL访问,例如,

http://localhost:3000/photos/1/small/1351546785_accepted_48.png 

回答

0

原来问题是image_file_name在数据库中存储为nil。

attr_accessor:image_file_name阻止image_file_name具有适当的值,因此必须将其删除。我已经添加了这一行,因为这个错误使得它似乎是必需的。为了摆脱错误,数据库迁移必须与更新:

def change 
    create_table :photos do |t| 
    t.string :image_file_name 
    t.string :image_content_type 
    t.integer :image_file_size 
    t.datetime :image_updated_at 

    t.timestamps 
    end 
end 
0

我有同样的问题,我的解决办法是这样的:

默认情况下您的生成器,你要添加的:照片属性来定义 “创造” 是这样的:

def create 
    @setting = Setting.new(setting_params) 
end 

只是它cange到:

def create 
    @setting = Setting.create(params[:setting]) 
end 

(只是要清楚;将设置更改为您自己的脚手架名称。)