2012-07-08 112 views
3

你好我一直在开发一个Web应用程序我想给用户上传个人资料图片的可能性。我花了很多时间试图获得载波和雾与s3工作,但没有设法实现它。你可以给我的任何帮助都非常赞赏。Rails图像上传到S3与carrierwave和雾

更新: 我一直试图摆弄它,从我可以告诉该文件永远不会上升,图像值始终为空的Users控制器。

我的上传者类。

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 

这是我能想到的,是相关的,如果有人需要我发布更多的代码,我会很高兴。 我真的很难倒,任何帮助表示赞赏。

+0

有没有在日志中的任何错误? – 2012-07-08 21:35:31

+0

您可能已将S3存储桶放置在错误的区域? – 2012-07-08 22:31:03

+0

克里斯托弗,S3桶在正确的区域。凯尔,没有任何错误。图像根本不会被上传。 – EnriqueC 2012-07-08 22:37:58

回答

0

我觉得你有与

if @photo.update_attributes(params[:image]) 

问题尝试这样的东西,而不是

if @photo.update_attributes(image: params[:photo][:image]) 
+0

谢谢,但它仍然无法正常工作 – EnriqueC 2012-07-08 22:40:15

相关问题