file-column学习

本文介绍了如何使用Rails框架中的file_column gem实现图片上传功能,并详细解释了如何在模型、视图中配置及显示上传的图片,同时提供了调整图片尺寸及自动生成不同版本的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、
Just make the "image" column ready for handling uploaded files...

class Entry < ActiveRecord::Base
file_column :image
end
#在model中指定一个要上传的字段

2、
... generate file fields that keep uploaded images during form redisplays to your view...

<%= file_column_field "entry", "image" %>
#在view中显示上传表单,
... and display uploaded images in your view:

<%= image_tag url_for_file_column("entry", "image") %>

#在view中显示上传文件

3、To resize every uploaded image to a maximum size of 640x480, you just have to declare an additional option.

class Entry < ActiveRecord::Base
file_column :image, :magick => { :geometry => "640x480>" }
end
#设置上传文件大小为640X480
4、
You can even automatically create versions in different sizes that have nice filenames...
class Entry < ActiveRecord::Base
file_column :image, :magick => {
:versions => { "thumb" => "50x50", "medium" => "640x480>" }
}
end

#建产一个versions,使每个不同大小的文件有不同的大小,如thumb=>"50X50","medium"=>"640X480"
<%= image_tag url_for_file_column 'entry', 'image' %>
#显示为原来大小
<%= image_tag url_for_file_column 'entry', 'image' ,'thumb'%>
#大小为thumb("50X50")
<%= image_tag url_for_file_column 'entry', 'image' ,'medium'%>
#大小为medium("640X480")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值