2012-11-04 24 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 

(只是要清楚;將設置更改爲您自己的腳手架名稱。)