我正在尝试在我的Rails 4.2.3应用程序上使用Paperclip和Amazon S3。
如果我使用Paperclip在我的机器上本地存储数据,一切正常。 当我尝试在S3上使用Paperclip存储文件时出现问题。
例如,只需尝试创建新的User实例就会触发错误:
u = User.new
u.name = "Mark"
u.save
NoMethodError: undefined method `new' for nil:NilClass
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/paperclip-4.3.6/lib/paperclip/attachment.rb:84:in `initialize'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/paperclip-4.3.6/lib/paperclip/has_attached_file.rb:47:in `new'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/paperclip-4.3.6/lib/paperclip/has_attached_file.rb:47:in `block in define_instance_getter'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validator.rb:149:in `block in validate'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validator.rb:148:in `each'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validator.rb:148:in `validate'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:455:in `public_send'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:455:in `block in make_lambda'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `call'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_validate_callbacks'
... 22 levels...
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
from /Users/Augusto/Sites/ccu_test/bin/rails:9:in `<top (required)>'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Augusto/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
from /Users/Augusto/.rvm/rubies/ruby-2.2.4/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Augusto/.rvm/rubies/ruby-2.2.4/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
我的Gemfile:
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem "pg"
gem 'aws-sdk', '~> 2.3'
gem "paperclip", '~> 4.3.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'jquery-rails'
gem 'jbuilder', '~> 2.0'
我安装了ImageMagik并添加到我的development.rb
Paperclip.options[:command_path] = "/usr/local/bin/"
我的模特:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => {:micro => "32x32#", :thumb => "50x50#", :big_thumb => "80x80#", :small => "220x220>", :featured => "220x220#", :medium => "600x600>"}#, :default_url => 'http://s3.amazonaws.com/ccu-static/file.small.png'
end
config&gt;初始化器&gt; paperclip_defaults.rb
module Paperclip
class Attachment
def self.default_options
@default_options ||= {
:storage => :s3,
:url => "/system/:attachment/:id/:style/:filename",
:path => "/:attachment/:id/:style/:filename",
:styles => {},
:processors => [:thumbnail],
:convert_options => {},
:default_url => "/images/missing.:style.gif", #"http://s3.amazonaws.com/ccu-static/missing.:style.png",
:default_style => :original,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
}
end
end
end
config&gt;初始化器&gt; aws.rb
Aws.config.update({
region: 'us-west-2',
credentials: Aws::Credentials.new('...', '...')
})
config&gt; S3.yml
production:
bucket: ...
access_key_id: ...
secret_access_key: ...
development:
bucket: ...
access_key_id: ...
secret_access_key: ...
schema.rb
create_table "users", force: :cascade do |t|
...
t.string "name"
t.string "avatar_file_name", limit: 255
t.integer "avatar_file_size"
t.string "avatar_content_type", limit: 255
t.datetime "avatar_updated_at"
end