2014-11-24 37 views
0

在開發中,使用CarrierWave進行Fog/S3上傳可以正常工作。在生產中,我一直看到這個錯誤:Heroku上的CarrierWave TypeError(無法轉儲文件)

我在其他堆棧溢出帖子上看到了這個,但它們是關於多部分表單的。我只是一個標準格式內的常規文件字段。我使用ActiveRecord的session_store(現在是一個單獨的gem),它看起來很難轉儲文件,但我不確定這是爲什麼。下面是完整的堆棧跟蹤:

2014-11-24T17:33:04.682703+00:00 app[web.1]: Processing by DefendantsController#update as HTML 
2014-11-24T17:33:04.682795+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"bhvn+IUv3Vw7OReBDDE97YZuRBBSs9bGQ6K1xvkTBpQ=", "defendant"=>{"organization_id"=>"2", "picture_attributes"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fdaab19c700 @tempfile=#<Tempfile:/tmp/RackMultipart20141124-2-65sl86>, @original_filename="avatar.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"defendant[picture_attributes][image]\"; filename=\"avatar.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "name"=>"john doe", "nickname_or_alias"=>"", "marital_status"=>"Single", "maiden_name"=>"", "birthday"=>"02/24/1984", "email"=>"[email protected]", "weight"=>"150", "height"=>"72", "skin_color"=>"White", "eye_color"=>"Brown", "gender"=>"Male", "race"=>"Caucasian", "hair_color"=>"Black", "scar_or_tattoo"=>"", "spouse_name"=>"", "facebook_url"=>"", "united_states_citizen"=>"1", "ssn"=>"000-11-1111"}, "commit"=>"Update Defendant", "id"=>"2"} 
2014-11-24T17:33:04.680411+00:00 app[web.1]: Started PATCH "/defendants/2" for 68.53.142.146 at 2014-11-24 17:33:04 +0000 
2014-11-24T17:33:05.543253+00:00 app[web.1]: Completed 302 Found in 860ms (ActiveRecord: 15.0ms) 
2014-11-24T17:33:05.542976+00:00 app[web.1]: Redirected to http://bailbetter.herokuapp.com/defendants/2/edit 
2014-11-24T17:33:05.549278+00:00 app[web.1]: 
2014-11-24T17:33:05.549283+00:00 app[web.1]: TypeError (can't dump File): 
2014-11-24T17:33:05.549284+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-session_store-0.1.0/lib/active_record/session_store.rb:7:in `dump' 
2014-11-24T17:33:05.549286+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-session_store-0.1.0/lib/active_record/session_store.rb:7:in `marshal' 
2014-11-24T17:33:05.549288+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-session_store-0.1.0/lib/active_record/session_store/session.rb:73:in `marshal_data!' 
2014-11-24T17:33:05.549289+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:424:in `block in make_lambda' 
2014-11-24T17:33:05.549291+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:160:in `call' 
2014-11-24T17:33:05.549292+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:160:in `block in halting' 
2014-11-24T17:33:05.549294+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call' 
2014-11-24T17:33:05.549295+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks' 
2014-11-24T17:33:05.549296+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/callbacks.rb:302:in `create_or_update' 
2014-11-24T17:33:05.549315+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/persistence.rb:103:in `save' 
2014-11-24T17:33:05.549316+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/validations.rb:51:in `save' 
2014-11-24T17:33:05.549318+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/attribute_methods/dirty.rb:21:in `save' 
2014-11-24T17:33:05.549319+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:268:in `block (2 levels) in save' 
2014-11-24T17:33:05.549320+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status' 
2014-11-24T17:33:05.549322+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction' 
2014-11-24T17:33:05.549323+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction' 
2014-11-24T17:33:05.549324+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction' 
2014-11-24T17:33:05.549325+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:208:in `transaction' 
2014-11-24T17:33:05.549327+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:326:in `with_transaction_returning_status' 
2014-11-24T17:33:05.549328+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:268:in `block in save' 
2014-11-24T17:33:05.549329+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:283:in `rollback_active_record_state!' 
2014-11-24T17:33:05.549331+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/transactions.rb:267:in `save' 
2014-11-24T17:33:05.549332+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-session_store-0.1.0/lib/action_dispatch/session/active_record_store.rb:80:in `block in set_session' 
2014-11-24T17:33:05.549333+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb:106:in `block (2 levels) in quietly' 
2014-11-24T17:33:05.549335+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb:48:in `silence_stream' 
2014-11-24T17:33:05.549336+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb:105:in `block in quietly' 
2014-11-24T17:33:05.549337+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb:48:in `silence_stream' 
2014-11-24T17:33:05.549338+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/core_ext/kernel/reporting.rb:104:in `quietly' 
2014-11-24T17:33:05.549340+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-session_store-0.1.0/lib/action_dispatch/session/active_record_store.rb:77:in `set_session' 
2014-11-24T17:33:05.549341+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:342:in `commit_session' 
2014-11-24T17:33:05.549342+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:226:in `context' 
2014-11-24T17:33:05.549343+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call' 
2014-11-24T17:33:05.549345+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/cookies.rb:560:in `call' 
2014-11-24T17:33:05.549346+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/query_cache.rb:36:in `call' 
2014-11-24T17:33:05.549352+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call' 
2014-11-24T17:33:05.549353+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
2014-11-24T17:33:05.549354+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:82:in `run_callbacks' 
2014-11-24T17:33:05.549356+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
2014-11-24T17:33:05.549357+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
2014-11-24T17:33:05.549358+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
2014-11-24T17:33:05.549359+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
2014-11-24T17:33:05.549361+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/rack/logger.rb:38:in `call_app' 
2014-11-24T17:33:05.549362+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/rack/logger.rb:20:in `block in call' 
2014-11-24T17:33:05.549363+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb:68:in `block in tagged' 
2014-11-24T17:33:05.549364+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb:26:in `tagged' 
2014-11-24T17:33:05.549365+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/tagged_logging.rb:68:in `tagged' 
2014-11-24T17:33:05.549367+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/rack/logger.rb:20:in `call' 
2014-11-24T17:33:05.549368+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/request_id.rb:21:in `call' 
2014-11-24T17:33:05.549369+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call' 
2014-11-24T17:33:05.549370+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call' 
2014-11-24T17:33:05.549372+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/activesupport-4.1.0/lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call' 
2014-11-24T17:33:05.553984+00:00 heroku[router]: at=info method=POST path="/defendants/2" host=bailbetter.herokuapp.com request_id=dc9adbf1-78de-48aa-b913-4b0f59992c0b fwd="68.53.142.146" dyno=web.1 connect=1ms service=978ms status=500 bytes=1497 
2014-11-24T17:33:05.549373+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/static.rb:64:in `call' 
2014-11-24T17:33:05.549374+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call' 
2014-11-24T17:33:05.549376+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/engine.rb:514:in `call' 
2014-11-24T17:33:05.549377+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/application.rb:144:in `call' 
2014-11-24T17:33:05.549378+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/content_length.rb:14:in `call' 
2014-11-24T17:33:05.549379+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/connection.rb:86:in `block in pre_process' 
2014-11-24T17:33:05.549380+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/connection.rb:84:in `catch' 
2014-11-24T17:33:05.549382+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/connection.rb:84:in `pre_process' 
2014-11-24T17:33:05.549383+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/connection.rb:53:in `process' 
2014-11-24T17:33:05.549384+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/connection.rb:39:in `receive_data' 
2014-11-24T17:33:05.549386+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine' 
2014-11-24T17:33:05.549387+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run' 
2014-11-24T17:33:05.549388+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/backends/base.rb:73:in `start' 
2014-11-24T17:33:05.549389+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/thin-1.6.2/lib/thin/server.rb:162:in `start' 
2014-11-24T17:33:05.549391+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run' 
2014-11-24T17:33:05.549392+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/server.rb:264:in `start' 
2014-11-24T17:33:05.549393+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands/server.rb:69:in `start' 
2014-11-24T17:33:05.549394+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:81:in `block in server' 
2014-11-24T17:33:05.549395+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `tap' 
2014-11-24T17:33:05.549396+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:76:in `server' 
2014-11-24T17:33:05.549398+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!' 
2014-11-24T17:33:05.549399+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>' 
2014-11-24T17:33:05.549400+00:00 app[web.1]: bin/rails:8:in `require' 
2014-11-24T17:33:05.549401+00:00 app[web.1]: bin/rails:8:in `<main>' 
2014-11-24T17:33:05.549402+00:00 app[web.1]: 
2014-11-24T17:33:05.549403+00:00 app[web.1]: 
2014-11-24T17:36:16.832243+00:00 app[web.1]: Started GET "/" for 54.87.115.122 at 2014-11-24 17:36:16 +0000 
2014-11-24T17:36:16.841057+00:00 app[web.1]: Rendered devise/sessions/new.html.erb within layouts/application (2.5ms) 
2014-11-24T17:36:16.842255+00:00 app[web.1]: Rendered shared/_errors.html.erb (0.1ms) 
2014-11-24T17:36:16.842361+00:00 app[web.1]: Rendered shared/_header.html.erb (0.3ms) 
2014-11-24T17:36:16.842586+00:00 app[web.1]: Rendered shared/_footer.html.erb (0.1ms) 
2014-11-24T17:36:16.835923+00:00 app[web.1]: Processing by Devise::SessionsController#new as */* 
2014-11-24T17:36:16.841969+00:00 app[web.1]: Rendered shared/_head.html.erb (0.7ms) 
2014-11-24T17:36:16.843465+00:00 app[web.1]: Completed 200 OK in 7ms (Views: 5.6ms | ActiveRecord: 0.0ms) 
2014-11-24T17:36:16.856746+00:00 heroku[router]: at=info method=GET path="/" host=bailbetter.herokuapp.com request_id=2624573d-1670-45d5-945d-13cbe8b99285 fwd="54.87.115.122" dyno=web.1 connect=1ms service=23ms status=200 bytes=4321 
2014-11-24T17:40:46.930531+00:00 app[web.1]: Started GET "/" for 54.211.161.144 at 2014-11-24 17:40:46 +0000 
2014-11-24T17:40:46.937803+00:00 app[web.1]: Rendered devise/sessions/new.html.erb within layouts/application (2.0ms) 
2014-11-24T17:40:46.933689+00:00 app[web.1]: Processing by Devise::SessionsController#new as */* 
2014-11-24T17:40:46.938622+00:00 app[web.1]: Rendered shared/_head.html.erb (0.7ms) 
2014-11-24T17:40:46.938908+00:00 app[web.1]: Rendered shared/_errors.html.erb (0.1ms) 
2014-11-24T17:40:46.939009+00:00 app[web.1]: Rendered shared/_header.html.erb (0.3ms) 
2014-11-24T17:40:46.939212+00:00 app[web.1]: Rendered shared/_footer.html.erb (0.1ms) 
2014-11-24T17:40:46.940133+00:00 app[web.1]: Completed 200 OK in 6ms (Views: 4.7ms | ActiveRecord: 0.0ms) 
2014-11-24T17:40:46.953041+00:00 heroku[router]: at=info method=GET path="/" host=bailbetter.herokuapp.com request_id=9a530552-a3bc-4401-a1b6-cc5736cd0c0b fwd="54.211.161.144" dyno=web.1 connect=10ms service=23ms status=200 bytes=4321 
2014-11-24T17:50:59.689993+00:00 heroku[router]: at=info method=GET path="/" host=bailbetter.herokuapp.com request_id=8cecda20-c0b9-48ac-b271-ab6d88deade7 fwd="54.90.222.139" dyno=web.1 connect=2ms service=17ms status=200 bytes=4321 
2014-11-24T17:50:59.671966+00:00 app[web.1]: Started GET "/" for 54.90.222.139 at 2014-11-24 17:50:59 +0000 
2014-11-24T17:50:59.678050+00:00 app[web.1]: Rendered devise/sessions/new.html.erb within layouts/application (1.8ms) 
2014-11-24T17:50:59.678987+00:00 app[web.1]: Rendered shared/_errors.html.erb (0.1ms) 
2014-11-24T17:50:59.679373+00:00 app[web.1]: Rendered shared/_footer.html.erb (0.1ms) 
2014-11-24T17:50:59.674640+00:00 app[web.1]: Processing by Devise::SessionsController#new as */* 
2014-11-24T17:50:59.678713+00:00 app[web.1]: Rendered shared/_head.html.erb (0.5ms) 
2014-11-24T17:50:59.679143+00:00 app[web.1]: Rendered shared/_header.html.erb (0.3ms) 
2014-11-24T17:50:59.680198+00:00 app[web.1]: Completed 200 OK in 5ms (Views: 4.2ms | ActiveRecord: 0.0ms) 

,這裏是控制器動作:

def update 
    @defendant = Defendant.find(params[:id]) 
    convert_params 

    unless @defendant.update_attributes(sanitized_params) 
     flash[:notice] = @defendant.errors.full_messages.to_sentence 
    end 

    redirect_to edit_defendant_path(@defendant) 
    end 

    private 

    def sanitized_params 
     params.require(:defendant).permit(
     :organization_id, :name, :maiden_name, :birthday, 
     :email, :weight, :height, :skin_color, 
     :eye_color, :gender, :race, :hair_color, 
     :scar_or_tattoo, :spouse_name, :facebook_url, :nickname_or_alias, 
     :marital_status, :united_states_citizen, :ssn, 
     picture_attributes: [ 
      :image 
     ] 
    ) 
    end 

    def convert_params 
     params[:defendant][:birthday] = convert_date_string(params[:defendant][:birthday]) 
    end 

UPDATE

下面是我使用Carrierwave初始化:

if Rails.env.test? 
    CarrierWave.configure do |config| 
    config.storage = :file 
    end 
else 
    CarrierWave.configure do |config| 
    config.storage = :fog 
    config.fog_credentials = { 
     :provider    => 'AWS',  
     :aws_access_key_id  => Rails.application.secrets.aws_access_key_id,  
     :aws_secret_access_key => Rails.application.secrets.aws_secret_access_key, 
     :region     => 'us-east-1' 
    } 
    config.fog_directory = "myapp-#{Rails.env}" 
    end 
end 

if Rails.env.test? 
    CarrierWave.configure do |config| 
    config.enable_processing = false 
    end 

    PictureUploader 

    CarrierWave::Uploader::Base.descendants.each do |klass| 
    next if klass.anonymous? 
    klass.class_eval do 
     def cache_dir 
     "#{Rails.root}/spec/support/uploads/tmp" 
     end 

     def store_dir 
     "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
     end 
    end 
    end 
end 

這裏是上傳者本身:

# encoding: utf-8 

class PictureUploader < CarrierWave::Uploader::Base 

    # Include RMagick or MiniMagick support: 
    # include CarrierWave::RMagick 
    include CarrierWave::MiniMagick 

    # Choose what kind of storage to use for this uploader: 
    # storage :file 
    # storage :fog 
    # DECLARED IN INITIALIZER 

    # Override the directory where uploaded files will be stored. 
    # This is a sensible default for uploaders that are meant to be mounted: 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    # Provide a default URL as a default if there hasn't been a file uploaded: 
    def default_url 
    # For Rails 3.1+ asset pipeline compatibility: 
    ActionController::Base.helpers.asset_path([version_name || "picture", "placeholder.png"].compact.join('_')) 

    # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 
    end 

    # Process files as they are uploaded: 
    # process :scale => [200, 300] 
    # 
    # def scale(width, height) 
    # # do something 
    # end 

    process resize_to_fit: [500, 500] 

    version :thumbnail do 
    process resize_to_fill: [50, 50] 
    end 

    # Add a white list of extensions which are allowed to be uploaded. 
    # For images you might use something like this: 
    # def extension_white_list 
    # %w(jpg jpeg gif png) 
    # end 

    # Override the filename of the uploaded files: 
    # Avoid using model.id or version_name here, see uploader/store.rb for details. 
    # def filename 
    # "something.jpg" if original_filename 
    # end 

end 

這裏的被告模型:

class Defendant < ActiveRecord::Base 
    include DateHelper 
    paginates_per 10 
    acts_as_paranoid 
    attr_encrypted :ssn, key: 'mykeygoeshere', encode: true 
    normalize_attribute :nickname_or_alias, with: :blank 
    normalize_attribute :marital_status, with: :blank 
    normalize_attribute :maiden_name, with: :blank 
    normalize_attribute :email, with: :blank 
    normalize_attribute :weight, with: :blank 
    normalize_attribute :height, with: :blank 
    normalize_attribute :skin_color, with: :blank 
    normalize_attribute :eye_color, with: :blank 
    normalize_attribute :race, with: :blank 
    normalize_attribute :hair_color, with: :blank 
    normalize_attribute :scar_or_tattoo, with: :blank 
    normalize_attribute :spouse_name, with: :blank 
    normalize_attribute :facebook_url, with: :blank 
    normalize_attribute :ssn, with: :blank 

    attr_accessor :forward_to_bond_creation 

    belongs_to :organization 
    has_many :bonds 
    has_many :court_dates 
    has_many :payments 
    has_one :picture 
    has_many :phones, as: :phoneable 
    has_many :addresses, as: :addressable 

    accepts_nested_attributes_for :picture, reject_if: proc { |attributes| attributes[:image].blank? } 
    accepts_nested_attributes_for :phones, reject_if: proc { |attributes| attributes.except(:phoneable_id).all?(&:blank?) } 
    accepts_nested_attributes_for :addresses, reject_if: proc { |attributes| attributes.except(:addressable_id).all?(&:blank?) } 

    validates_presence_of :organization, :name, :birthday, :gender 
    validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, allow_nil: true 
    validates_format_of :ssn, :with => /\A(\d{3}-?\d{2}-?\d{4})\Z/, allow_nil: true 
    validates_format_of :facebook_url, :with => /facebook.com/, allow_nil: true 

    def behind_with_payments? 
    !!(payment_status =~ "behind_with_payments") 
    end 

    def current_with_payments? 
    !!(payment_status =~ "current_with_payments") 
    end 

    def payment_status 
    if bonds.behind_with_payments.any? 
     "behind_with_payments" 
    else 
     "current_with_payments" 
    end 
    end 

    def picture_url 
    if picture 
     picture.image.url 
    else 
     ActionController::Base.helpers.asset_path("picture_placeholder.png") 
    end 
    end 

    def thumbnail_url 
    if picture 
     picture.image.thumbnail.url 
    else 
     ActionController::Base.helpers.asset_path("thumbnail_placeholder.png") 
    end 
    end 

    def as_json options={} 
    options.merge({ id: id, name: name, birthday: format_date(birthday), thumbnail_url: thumbnail_url }) 
    end 
end 

這裏是圖片型號:

class Picture < ActiveRecord::Base 
    acts_as_paranoid 

    belongs_to :defendant 

    mount_uploader :image, PictureUploader 
end 

更新2

我刪除了activerecord-session_store寶石,心想可能成爲問題,因爲那是錯誤發生的地方。我還添加了這條線,該條線可以幫助其他人在類似的情況下(儘管我的最佳直覺,因爲圖像沒有被保存,但我知道它不僅在重定向之前發生了錯誤,而且我試了一下):

params[:defendant][:picture_attributes][:image].tempfile = nil 

甚至與這兩個補丁,我得到了同樣的錯誤:

2014-11-25T19:49:39.916837+00:00 app[web.1]: TypeError (can't dump File): 
2014-11-25T19:49:39.916842+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/cookies.rb:418:in `serialize' 
2014-11-25T19:49:39.916839+00:00 app[web.1]: vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/middleware/cookies.rb:418:in `dump' 
... 
+0

如果您要提交文件,則必須使用多部分表單。這是'multipart/form-data'的目的。另外,我真的很希望你計劃在加密過程中大量使用'ssn'。這裏是[W3關於multipart/form-data的信息](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2) – engineersmnky 2014-11-24 18:45:02

+0

@engineersmnky的確,SSN是加密的:)我沒有意識到這一點。萬分感謝!我會讓你知道它是否有效。 – trevorhinesley 2014-11-24 19:06:00

+0

@engineersmnky沒有解決它:/ – trevorhinesley 2014-11-24 19:09:38

回答

1

也許這將幫助別人......我試着上傳在控制檯的圖像,並實現了我的AWS關鍵是越來越設置爲:

ENV["AWS_ACCESS_KEY_ID"] 

又名我的secrets.yml文件看起來像:

production: 
    secret_key_base: ENV["SECRET_KEY_BASE"] 
    aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'] 
    aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] 

因此,所有的錯誤。在我secrets.yml文件改成了:

production: 
    secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 
    aws_access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %> 
    aws_secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %> 

現在我們都好。對不起,夥計們,謝謝!

+0

,這是合理的,因爲它在'YAML'之前通過'erb'。 – engineersmnky 2014-11-25 21:18:13

+0

@engineersmnky雅它只是讀我作爲原始字符串,而不是抓住實際的ENV變量。 – trevorhinesley 2014-11-25 21:45:01

+0

也只是因爲我注意到你如何測試而不是創建一堆無用的文件在本地磁盤上進行測試。看看[Mocking Fog](https://blog.engineyard.com/2011/mocking-fog-when-using-it-with-carrierwave)。這樣做的事情已經清理了我們的測試環境了很多。 – engineersmnky 2014-11-26 14:17:42