我在Rails 4.2中重写了一个旧的Rails 2.3应用程序,保留了数据库内容。
到目前为止一切顺利。
旧应用程序使用Paperclip 2.3插件并将文件存储在S3上。 在新的应用程序上,我安装了Paperclip 4.3.6 gem和aws-sdk 2.3。
我的问题:当我为任何现有用户调用头像时,例如
User.first.avatar
应用返回:
undefined method `new' for nil:NilClass
我已经安装了ImageMagik并添加了
Paperclip.options[:command_path] = "/usr/local/bin/"
到我的development.rb
以下是我的型号/控制器:
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'
validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/jpg', 'image/png', 'image/gif'], :if => lambda {|user| user.avatar_file_name}
end
users_controller.rb
class UsersController < ApplicationController
private
def user_params
params.require(:user).permit(:avatar)
end
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; 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 "avatar_file_name", limit: 255
t.integer "avatar_file_size"
t.string "avatar_content_type", limit: 255
t.datetime "avatar_updated_at"
end
视图:
<%= image_tag(@top_players_score[0].avatar.url(:thumb)) if @top_players_score[0].avatar %>
无论如何,即使从具有基本功能的控制台我也会得到同样的错误:
user.avatar