2014-01-31 206 views
1

我已經爲我的紅寶石軌道項目安裝了回形針。但我不能上傳多個圖像。 我有兩個不同的領域,我想上傳圖像爲eg logo and picture.回形針上傳多個圖像,重命名名稱「頭像」

我可以更改名稱「頭像」,以表明字段的名稱?可能嗎?

+0

它是否適用於單張圖片上傳? –

+1

[用回形針上傳多個文件]的可能的重複(http://stackoverflow.com/questions/11605787/uploading-multiple-files-with-paperclip) –

回答

1

是的,這是可能的。

如果你想改變「阿凡達」「標誌」使用下面的命令

rails g paperclip modalname logo 

,其中的標誌是你的字段名。

我希望你能理解,可能會解決你的問題。

0

這聽起來像你需要一些想法與Rails & Paperclip工作:


回形針

處理一個文件附件&將數據發送到您的數據庫

- 創建數據庫條目:

your_definition_file_name 
your_definition_content_type 
your_definition_file_size 
your_definition_uploaded_at 

- 處理的對象稱爲_your_definition

#app/models/attachment.rb 
Class Attachment < ActiveRecord::Base 
    has_attached_file :your_definition 
end 

回形針基本上就像你的數據庫&文件之間的橋樑 - 這意味着你只需要保留對象名稱一致,以得到它的工作


代碼

如果你有兩個字段(logo & picture),你需要聲明它們在你的附件模型,通過他們的參數,可以在你的表添加列:

#app/controllers/attachments_controller.rb 
def create 
    @attachment = Attachment.new(attachment_params) 
    @attachment.save 
end 

private 

def attachment_params 
    params.require(:attachment).permit(:logo, :picture) 
end 

#app/models/attachment.rb 
Class Attachment < ActiveRecord::Base 
    has_attached_file :logo 
    has_attached_file :picture 
end 

#db/migrate 
def change 
    add_attachment :attachments, :logo 
end 

attachments 
id | logo_file_name | logo_content_type | logo_file_size | logo_uploaded_at | picture_file_name | picture_content_type | picture_file_size | picture_uploaded_at | created_at | updated_at 

建議

上面的代碼是不是很DRY

我會建議使用您的附件模型type屬性,每段時間設定類型上傳

這樣,您就可以撥打attachment作爲image或相似,具有type額外對每次上傳:

#app/controllers/attachments_controller.rb 
def create 
    @attachment = Attachment.new(attachment_params) 
    @attachment.save 
end 

private 

def attachment_params 
    params.require(:attachment).permit(:image, :type) 
end 

#app/models/attachment.rb 
Class Attachment < ActiveRecord::Base 
    has_attached_file :image 
end 

#db/migrate 
def change 
    add_attachment :attachments, :logo 
end 

attachments 
id | image_file_name | image_content_type | image_file_size | image_uploaded_at | type | created_at | updated_at