EnriqueC
EnriqueC

Reputation: 61

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. I spent a lot of time trying to get carrierwave and fog working with s3 but have not managed to accomplish it. 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.

Upvotes: 3

Views: 3008

Answers (2)

EnriqueC
EnriqueC

Reputation: 61

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

Upvotes: 2

Christopher Lindblom
Christopher Lindblom

Reputation: 1376

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])

Upvotes: 0

Related Questions