【问题标题】:Rails Image Upload to S3 with carrierwave and fog使用carrierwave和fog将Rails图像上传到S3
【发布时间】:2012-07-08 08:51:46
【问题描述】:

您好,我一直在开发一个网络应用程序,我希望让用户能够上传个人资料图片。我花了很多时间试图让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

这就是我能想到的所有相关内容,如果有人需要我发布更多代码,我会很乐意。 老实说,我很难过,感谢任何帮助。

【问题讨论】:

  • 您的日志中是否有任何错误?
  • 您可能将 S3 存储桶放在了错误的区域?
  • Christopher,S3 存储桶位于正确的区域。凯尔,没有任何错误。图片永远不会上传。
  • 您是否尝试过在@photo = Photo.find(params[:id]) 之后直接放置一个调试器语句,然后从调试器启动 irb 并尝试手动执行?这样你可能会发现问题。

标签: ruby-on-rails ruby amazon-s3 carrierwave fog


【解决方案1】:

我设法为任何感兴趣的人找出问题所在。事实上,我正在与我的用户一起使用设计,并且需要在上传表单中进行一些不同的配置才能使其正常工作。这是帮助我的文档的链接,以防其他人遇到此问题。

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

【讨论】:

    【解决方案2】:

    我觉得你有问题

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

    试试这样的方法

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

    【讨论】:

      猜你喜欢
      • 2016-06-29
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2014-06-28
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      相关资源
      最近更新 更多