2014-01-31 85 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