获取:使用多个Paperclip附件保存模型时未定义的方法`stringify_keys'。 (RoR,Paperclip,S3)

时间:2012-09-05 05:02:03

标签: ruby-on-rails ruby amazon-s3 paperclip

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

 :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:在`update'

这种情况发生在我的本地Windows 7计算机上,使用rails服务器,尝试上传到我的S3存储桶。


原始问题:

我正在尝试使用paperclip上传多个图像(资源),并将它们与我的用户模型一起保存在控制器更新操作上。     我跟着这个Paperclip S3 Totorial: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文件中的资产模型:

     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

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

提前致谢, 亚历

1 个答案:

答案 0 :(得分: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