gem-carriewave

carriewave是一个可以帮助我们上传我们需要文件的插件。它可以上传文档,图片,且可以一次上传多张图片。
1.上传文档
(1)在gemfile中添加 gem “carriewave”
bundle install
重开sever:ctrl+c => rails s
(2)加入uploader机制
rails g uploader xxx
eg:rails g uploader attachment

(3)在上传的Model中
mount_uploader :xxx,XxxUploader
eg:mount_uploader :attachment,AttachmentUploader
(4)在需要的view(new)页面中添加 <%= f.input :xxx %>
(5)修改.gitignore,加入public/uploads
(6)在关联的model中加上栏位 => rake db:migrate
eg:attatchment:string
(7)新建一个controller
eg:

class ResumesController < ApplicationController
before_action :authenticate_user!

def new
  @job = Job.find(params[:job_id])
  @resume = Resume.new
end

def create
@job = Job.find(params[:job_id])
@resume = Resume.new(resume_params)
@resume.job = @job
@resume.user = current_user
if @resume.save
  flash[:notice]="Upload Success"
  redirect_to job_path(@job)
else
  render :new
end
end

private
def resume_params
params.require(:resume).permit(:content,:attachment)
end

end
(8)在routes里加上 resources :xxx

2.添加图片(一张)
(1)在gemfile中添加
gem “carriewave”
gem “mini_magick”
然后
bundle install
rails g uploader image
重开sever
(2)在相关model中添加栏位 image :string => rake db:migrate
(3)在 Model 中添加 mount_uploader :image,ImageUploder
(4)在image_uploader中 ”include Carriewave::Minigack“ 前面的#删掉
在Store_dir end下加

process resize_to_fit:[800,800]
version :thumb do
  process resize_to_fit:[200,200]
end
version :medium do
  process resize_to_fit:[400,400]
end

(5)View:
new

 <%= f.input :image,as::file %>

 edit

 <% if @xxx.image.present? %>
 <span>目前商品图片</span>
 <%= image_tag (@xxx.image.thumb.url) %>
 <% end %>

 show
 index
 <% @products.each do |product| %>
<div class="col-xs-6 col-md-3">
<%= link_to product_path(product) do%>
<% if product.image.present? %>
<%= image_tag(product.image.thumb.url, class:"thumbnail")%>
<% else %>
<%= image_tag("http://placehold.it/200x200&text=No pic",class:"thumbnail")%>
<% end %>
<% end %>
show页面在循环中的product前面加上@

3.上传多张图片 教程1教程2中文