简体   繁体   English

Rails Image使用carrierwave和fog上传到S3

[英]Rails Image Upload to S3 with carrierwave and fog

Hello I've been developing a web application I and want to give users the possibility to upload profile pictures. 你好我一直在开发一个Web应用程序我想让用户上传个人资料图片。 I spent a lot of time trying to get carrierwave and fog working with s3 but have not managed to accomplish it. 我花了很多时间试图让运载波和雾与s3一起工作,但还没有成功完成它。 Any help you can give me is much apreciated. 你能给我的任何帮助都是非常苛刻的。

Update: Ive kept trying to fiddle around with it and from what i can tell the file is never uplaoding, the :image value is always empty in the Users controller. 更新:我一直试图摆弄它,从我可以告诉文件永远不会上传,:图像值在用户控制器中始终为空。

My uploader class. 我的上传课程。

class PhotoUploader < CarrierWave::Uploader::Base
   storage :fog
   def store_dir
      "uploads/#images/#{model.class.to_s.underscore}"
   end
end

The fog initializer 雾初始化器

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 

User model 用户模型

class User
  include Mongoid::Document
  ....
  has_one :photo
  ....
end

Photo Model 照片模型

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

Photo Controller 照片控制器

  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

Upload form 上传表格

= 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

This is all I can think of that is relevant, if anyone need me to post more code I'll be happy to. 这是我能想到的所有相关内容,如果有人需要我发布更多代码我会很高兴。 I am honestly stumped and any help is appreciated. 老实说,我很难过,任何帮助都表示赞赏。

I managed to figure out the issue for anyone interested. 我设法为任何感兴趣的人找出问题。 It was the fact that I was using devise with my user and there needed to be some different configuration in the upload forms in order for it to work correctly. 事实上,我正在使用设备与我的用户,并且需要在上传表单中有一些不同的配置才能使其正常工作。 Here is the link to the documentation that helped me in case there are others with the issue. 以下是帮助我的文档的链接,以防有其他问题。

https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-carrierwave-with-devise https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-carrierwave-with-devise

I think you have a problem with 我觉得你有问题

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

try something like this instead 尝试这样的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM