用cloudinary将照片上传到网站 - 需要绝对路径

时间:2014-07-20 17:50:46

标签: ruby-on-rails ruby-on-rails-3 activerecord carrierwave cloudinary

我正在使用rails 3通过cloudinary和carrierwave将照片上传到我的服务器。

class NutsController < ApplicationController
  include UsersHelper
  include NutsHelper

  def index
    if current_user
      @user = User.find(session[:user_id])
      @nuts = Nut.all
    else
      redirect_to :root
    end
  end

  def new
    # current_user
  end

  def show
  end

  def create
    if params[:file].present?
      @upload = Cloudinary::Uploader.upload(params[:file])
      @picture = @upload["secure_url"]
    end

    @nut = Nut.new(url: [@picture])
    if @nut.save
      redirect_to user_path(current_user)
    end
  end
end

&LT; ------------------------------------------&GT; < / p>

<%= cloudinary_js_config %>
<h1>Create a Nushell</h1>
<%= form_for :nut, url: nuts_path do |f| %>

  <%= cl_image_upload_tag(:image_id) %>

  <%= f.submit "Nut it Up!"%>
<% end %>

class PictureUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave

  storage :file

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

我的文件名为Yellow_Happy.jpg。当我打印出params [:file]时,它返回&#39; Yellow_Happy.jpg&#39;。要上传到cloudinary我需要整个路径我的文件,即Users / apprentice / Desktop / Yellow_Happy.jpg。有谁知道如何指定我的文件的整个路径?

1 个答案:

答案 0 :(得分:1)

如果你查看你的上传代码,你已经指定storage :file ,它基本上告诉carrierwave在你的应用程序中上传图像并且 store_dir函数告诉你上传图像的位置。您甚至可以在app/public/uploads内查看。有关详细信息,请结帐carrierwave documentation

如果您查看cloudinary documentation,则只需在您的上传器中包含包含Cloudinary :: CarrierWave (除非您使用的是缩略图或其他载波功能)。你的上传者应该是这样的:

class PictureUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave

  #storage :file  #You don't need it

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