0
votes

EDIT: The below issue was resolved by including the s3 credentials directly in the Paperclip model (asset), like this:

 :s3_credentials => { :access_key_id     => 'AAAAAAAAAOKSSISVNQ', 
                       :secret_access_key => 'AAAAAAAAAAAAAAAAAAA1eWYh0au8Pg4bOnAmmX' ,
                       :bucket => 'my_bucket_name' },

A new issue now occurs, when uploading the image to S3: ** AWS::Core::Client::NetworkError ** app/controllers/users/registrations_controller.rb:9:in `update'

This happens on my local Windows 7 machine, with rails server, trying to upload to my S3 bucket.


Original Issue:

I am trying to upload multiple images (assets) using paperclip, and save them along with my user model on the controller update action. I followed this Paperclip S3 Totorial: http://webtempest.com/how-to-allow-image-uploads-in-rails-on-heroku/

I've found 10 or so different answers on this exact error with Paperclip, but none of them solved my problem.

If i comment out S3 config lines from my Asset model, the error goes away.

The error happens in the update action in my registration_controller:

**undefined method `stringify_keys' for #<String:0x2a522d8>
    app/controllers/users/registrations_controller.rb:8:in `update'**

    {"utf8"=>"✓",
     "_method"=>"put",
     "authenticity_token"=>"ACFzngW4IXpuUDXutwHDCbpCuTjx2sFrZpcwBqt31LU=",
     "user"=>{"nickname"=>"newuser1",
     "first_name"=>"111",
     "last_name"=>"222",
     "birth_date"=>"2012-01-01 00:00:00.000000",
     "email"=>"[email protected]",
     "assets_attributes"=>{"0"=>{"asset"=>#<ActionDispatch::Http::UploadedFile:0x2a97a70 @original_filename="CIMG6275.JPG",
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"user[assets_attributes][0][asset]\"; <br/>filename=\"CIMG6275.JPG\"\r\nContent-Type: image/jpeg\r\n",
     @tempfile=#<File:C:/Users/AA/AppData/Local/Temp/RackMultipart20120905-7504-7sivk1>>}},
     "instrument_ids"=>["1",
     "2",
     "3"],
     "id"=>"9"},
     "commit"=>"Update"}

Here the part of my User model that matters:

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable 
      attr_accessible :id, :email, :password, :password_confirmation, :remember_me, 
           first_name, :last_name, :birth_date, :nickname, :instrument_ids, :assets,:assets_attributes 

      has_many :assets

      accepts_nested_attributes_for :assets, :allow_destroy => true
      ...

The asset model (this is the attachment paperclip object):

    class Asset < ActiveRecord::Base
      belongs_to :user
      has_attached_file :asset, 
    :whiny => false, 
          :styles => { :large=>"640x480g", :medium => "300x300>", :thumb => "100x100>" },
          :storage => :s3,
          :s3_credentials => "#{Rails.root}/config/s3.yml",
          :path => ":attachment/:id/:style.:extension",
          :bucket => 'name_of_my_bucket'
    end

My controller update code:

    class Users::RegistrationsController < Devise::RegistrationsController
      public 
      def update
        params[:user][:instrument_ids] ||= []
        @user = User.find(params[:user][:id])   
         if @user.update_without_password(params[:user])   
            respond_to do |format|
              format.html { redirect_to root_path }
              format.xml  { head :ok }
            end
          else
            respond_to do |format|
              format.html { render :action => "edit", :layout => "dialog" }
              format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
            end
          end
      end
      ...

The part of the user view that matters:

    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }) do |f| %> 
      <%= devise_error_messages! %>
        <% counter = 1 %>
        <%= f.fields_for :assets do |asset_fields| %>
            <% if asset_fields.object.new_record? %>
                <%= f.label "Image #{counter}" %>
                <% counter = counter + 1 %>
                <%= asset_fields.file_field :asset %>
                <%= asset_fields.label :asset_file_name %>
            <% end %>
        <% end %>

The Asset model in the schema.rb file:

     create_table "assets", :force =&gt; true do |t|
        t.string   "asset_file_name"
        t.string   "asset_content_type"
        t.integer  "asset_file_size"
        t.datetime "asset_updated_at"
        t.integer  "user_id"
        t.datetime "created_at",         :null =&gt; false
        t.datetime "updated_at",         :null =&gt; false
      end

And finally, part of the gemfile:

    gem 'jquery-rails'
    gem 'aws-s3'
    gem 'aws-sdk'
    gem 'devise'
    gem 'cancan'
    gem 'omniauth-facebook'
    gem 'omniauth'
    gem 'roo'
    gem 'client_side_validations'
    gem 'sqlite3'
    gem 'nifty-generators'
    gem 'paperclip'



s3.yml: access_key_id: AKUAJO4RGQ4TKSSIQVNB
secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
bucket: my_bucket_name

i modified a few chars in the above just for security purposes

Thanks in advance, Alex

1
please, provide "#{Rails.root}/config/s3.yml"sumskyi
access_key_id: AKUAJO4RGQ4TKSSIQVNB secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY<br/> bucket: my_bucket_name<br/> #(i modified a few chars in the above just for security purposes)FranticRock
Did you solve your problem? I've the similar error!Azzurrio

1 Answers

0
votes

It seems, you need to define proper environments in s3.yml

Edit s3.yml file from:

access_key_id: AKUAJO4RGQ4TKSSIQVNB 
secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
bucket: my_bucket_name

to:

development:
  access_key_id: AKUAJO4RGQ4TKSSIQVNB 
  secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
  bucket: my_bucket_name


production:
  access_key_id: AKUAJO4RGQ4TKSSIQVNB 
  secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
  bucket: my_bucket_name