在开发过程中,Fog / S3上传可与CarrierWave一起使用。在制作中,我一直看到这个错误:
我在其他Stack Overflow帖子上看过这个,但它们是关于多部分表单的。我只是标准表单中的常规文件字段。我使用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"=>"blah@blah.com", "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
更新
这是我用于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 :(得分:1)
也许这会帮助其他人......我尝试在控制台中上传图像,并意识到我的AWS密钥设置为:
ENV["AWS_ACCESS_KEY_ID"]
Aka my 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'] %>
现在我们都很好。对不起,伙计们,谢谢你!