您好,我一直在开发一个网络应用程序,我希望让用户能够上传个人资料图片。我花了很多时间试图让carrierwave和fog与s3一起工作,但没有成功。非常感谢您能给我的任何帮助。
更新:我一直试图摆弄它,据我所知,文件永远不会上传,用户控制器中的 :image 值始终为空。
我的上传者类。
class PhotoUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#images/#{model.class.to_s.underscore}"
end
end
雾初始化器
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'xxx',
:aws_secret_access_key => 'yyy',
}
config.fog_directory = 'pictures_dev'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
用户模型
class User
include Mongoid::Document
....
has_one :photo
....
end
照片模型
class Photo
include Mongoid::Document
attr_accessible :name, :image
field :image, :type => String
field :name, :type => String
belongs_to :user
mount_uploader :image, PhotoUploader
end
照片控制器
class PhotoController < ApplicationController
def update
@photo = Photo.find(params[:id])
if @photo.update_attributes(params[:image])
flash[:success] = "Your have updated your settings successfully."
else
flash[:error] = "Sorry! We are unable to update your settings. Please check your fields and try again."
end
redirect_to(:back)
end
上传表格
= form_for @photo, :html => {:multipart => true} do |f|
%p
%label Photo
= image_tag(@user.image_url) if @photo.image?
= f.file_field :image
= f.submit
这就是我能想到的所有相关内容,如果有人需要我发布更多代码,我会很乐意。老实说,我很难过,任何帮助表示赞赏。