Gem 关于 CarrierWave 的图片的存储位置及显示问题

tardis · 2015年03月26日 · 最后由 linjunzhugg 回复于 2015年03月27日 · 5564 次阅读

avatar_uploader.rb

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
   include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
#    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
#    "uploads/company/#{Time.now.to_i.to_s}"
    'public/upload/'
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
   def default_url
     "avatar/#{version_name}.png"
   end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [50, 50]
  # end

  version :normal do
    process resize_to_fill: [48, 48]
  end

  version :large do
    process resize_to_fill: [64, 64]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
   def extension_white_list
     %w(jpg jpeg gif png)
   end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

new.html.erb

<%= form_for(@company)  do |f| %>
  <%= render 'shared/company_error_messages'%>

  <%= f.file_field :avatar %>
  <%= f.hidden_field :avatar_cache %>
<% end %>

show.html.erb

<%= image_tag(@company.avatar_url(:large)) if @company.avatar %>

controller

class CompaniesController < ApplicationController
  def index
    @companies = Company.paginate(page: params[:page], per_page: 6)
      respond_to do |format|
        format.html
        format.json {render json: {company: @companies}}
      end
  end

  def new
    @company = Company.new
  end

  def show
    @company = Company.find(params[:id])
    @company_jobs = @company.company_jobs.paginate(page: params[:page])
  end

  def create
    @company = Company.new(company_params)
    @company.avatar = params[:file]
    @company.avatar = [File.open("public/uploads/")]
    if @company.save
      redirect_to @company
    else
      render 'new'
    end
  end

  def update

  end

  private
    def company_params
      params.require(:company).permit(:name, :describe, :contact,
                                      :product, :location, :field,
                                      :homepage, :funder,
                                      :address)
    end
end

现在每次在 show 页面中显示的都是 default_url 定义的图片,好像 store_dir 并没有起作用。

我的问题是,在 store_dir 方法中,图片的存储路径怎么写,是要自己创建文件夹吗,如果是文件夹要在哪里创建。 菜鸟求教,谢谢

store_dir方法中的路径不需要自己去创建,你可以看看public/upload文件夹里有没有已经上传过的图片

#1 楼 @huopo125 store_dir 默认路径是

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

但是上传后没找到图片,不知道怎么回事

你的 model 保存过么?没保存是在 public/upload/tmp 下面。

#2 楼 @tardis

如果你不设置这段

def store_dir
#    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
#    "uploads/company/#{Time.now.to_i.to_s}"
    'public/upload/'
  end

默认路径确实是你说的那样,此时应该是有的。

但你设置了这段

def store_dir
#    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
#    "uploads/company/#{Time.now.to_i.to_s}"
    'public/upload/'
  end

所有图片都「没有名字」保存在 upload?

show your controller code

#2 楼 @tardis 你可以通过 console 查看数据库,看看路径,比对一下;若找不到,那就没有上传成功。

#5 楼 @linjunzhugg

class CompaniesController < ApplicationController
  def index
    @companies = Company.paginate(page: params[:page], per_page: 6)
#      render json:@companies
      respond_to do |format|
        format.html
        format.json {render json: {company: @companies}}
      end
#    render json: {company: @companies}
  end

  def new
    @company = Company.new
  end

  def show
    @company = Company.find(params[:id])
#    current_company = @company
#    @company_job = @company.company_jobs.build
    @company_jobs = @company.company_jobs.paginate(page: params[:page])
  end

  def create
    @company = Company.new(company_params)
    @company.avatar = params[:file]
    @company.avatar = [File.open("public/uploads/")]
    if @company.save
      redirect_to @company
    else
      render 'new'
    end
  end

  def update

  end

  private
    def company_params
      params.require(:company).permit(:name, :describe, :contact,
                                      :product, :location, :field,
                                      :homepage, :funder,
                                      :address)
    end
end

#7 楼 @tardis

额,首先。你上传文件,文件是保存在暂存区 tmp 目录下,当你执行 save 保存时,才会保存在指定的目录,默认是:"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

第二,你的指定目录已经有问题了,public/uploads/ 是什么鬼,这样相当于没保存到。你可以用着默认目录先。

第三,使用 carriwave 上传时,不需要你指定

@company.avatar = params[:file]
@company.avatar = [File.open("public/uploads/")]

写到

def company_params
     params.require(:company).permit(:name, :describe, :contact,
                                     :product, :location, :field,
                                     :homepage, :funder,
                                     :address, :avatar, :avatar_cache)
   end

之后 save 就可以自动保存了。

建议你看清楚文档后在写

https://github.com/carrierwaveuploader/carrierwave

需要 登录 后方可回复, 如果你还没有账号请 注册新账号