rails gem系列之CarrierWave

##概要
CarrierWave是rails中用来上传文件的gem。类似的gem还有PaperClip。

##和PaperClip的区别
1.PaperClip简单易用

2.CarrierWave的功能较多,应用广泛

##用法

先来做一个测试用的project吧

1
2
3
4
5
rails new test
cd test
rails g scaffold Product name:string
rake db:migrate
rails s

加入CarrierWave的gem

Gemfile
1
gem 'carrierwave'

安装

1
bundle install

生成carrierwave的配置文件

1
2
rails g uploader Image
create app/uploaders/image_uploader.rb

加入image列

1
2
rails g migration add_image_to_product image:string
rake db:migrate

向model中加入mount_uploader方法

1
2
3
4
# app/models/product.rb
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
end

向form中加入上传组件,其中指定hidden属性image_cache的作用就是当提交form时发生error上传的文件或者图片可以保存起来,不用再上传一次。

_form.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 开始 -->
<div class="field">
<% if @product.image? %>
<div class="thumbnail">
<%= image_tag @product.image.url %>
</div>
<% end %><br>
<%= f.label :image %><br>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field">
<!-- 既存product且存在图片-->
<% if @product.persisted? && @product.image? %>
<label>
<%= f.check_box :remove_image %>
删除
</label>
<% end %>
</div>
<!-- 结束 -->

别忘了要在controller中加一下StrongParameter

products_controller.rb
1
2
3
def product_params
params.require(:product).permit(:name, :price, :image, :image_cache, :remove_image)
end

添加显示部分的image_tag代码

show.html.erb
1
2
3
4
5
6
7
8
<p>
<strong>Image:</strong>
<% if @product.image? %>
<div class="thumbnail">
<%= image_tag @product.image.url %>
</div>
<% end %>
</p>

就这些,大功告成,很简单。