簡體   English   中英

Rails Image使用carrierwave和fog上傳到S3

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

你好我一直在開發一個Web應用程序我想讓用戶上傳個人資料圖片。 我花了很多時間試圖讓運載波和霧與s3一起工作,但還沒有成功完成它。 你能給我的任何幫助都是非常苛刻的。

更新:我一直試圖擺弄它,從我可以告訴文件永遠不會上傳,:圖像值在用戶控制器中始終為空。

我的上傳課程。

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

這是我能想到的所有相關內容,如果有人需要我發布更多代碼我會很高興。 老實說,我很難過,任何幫助都表示贊賞。

我設法為任何感興趣的人找出問題。 事實上,我正在使用設備與我的用戶,並且需要在上傳表單中有一些不同的配置才能使其正常工作。 以下是幫助我的文檔的鏈接,以防有其他問題。

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

我覺得你有問題

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

嘗試這樣的事情

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM