0

编辑:通过将 s3 凭据直接包含在回形针模型(资产)中解决了以下问题,如下所示:

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

将图像上传到 S3 时,现在出现了一个新问题:** AWS::Core::Client::NetworkError ** app/controllers/users/registrations_controller.rb:9:in `update'

这发生在我的本地 Windows 7 机器上,带有 rails 服务器,试图上传到我的 S3 存储桶。


原始问题:

我正在尝试使用回形针上传多个图像(资产),并将它们与我的用户模型一起保存在控制器更新操作中。我跟着这个回形针 S3 教程: http ://webtempest.com/how-to-allow-image-uploads-in-rails-on-heroku/

关于 Paperclip 的这个确切错误,我找到了 10 个左右不同的答案,但没有一个能解决我的问题。

如果我从我的资产模型中注释掉 S3 配置行,错误就会消失。

错误发生在我的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"=>"my_email@gmail.com",
     "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"}

这是我的用户模型中重要的部分:

    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
      ...

资产模型(这是附件回形针对象):

    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

我的控制器更新代码:

    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
      ...

重要的用户视图部分:

    <%= 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 %>

schema.rb 文件中的 Asset 模型:

     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

最后,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

我在上面修改了几个字符只是为了安全起见

提前致谢, 亚历克斯

4

1 回答 1

0

看来,您需要在 s3.yml 中定义适当的环境

从以下位置编辑 s3.yml 文件:

access_key_id: AKUAJO4RGQ4TKSSIQVNB 
secret_access_key: UGiDBv2rohLJdIHNSQK3N1eWYh0au8Pg4bOnAxxY
bucket: my_bucket_name

至:

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
于 2012-09-05T14:40:39.743 回答