使用回形针 gem 时出错(before_avatar_post_process)。我正在使用 rails 4.2 和 ruby 2

Error(before_avatar_post_process) using paperclip gem. I am using rails 4.2 and ruby 2

我是 rails ruby 的初学者。我 运行 在使用回形针 gem 时遇到错误。

用户模型是

class User < ActiveRecord::Base
has_secure_password

attr_accessor :validate_password

has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy
has_many :replys, dependent: :destroy

has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/



validates :registered_id, :presence => true, :uniqueness => true, :length => { :is => 10 }, 
                                                                  :on => :create
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
validates :password, :length => {:in => 6..20},  if: lambda { |u| u.validate_password == true }
end

迁移由命令行生成

"rails generate paperclip user profile_picture"

当我删除行时

validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

在用户模型中,它不显示错误。如果我添加该行它在

中显示错误
NoMethodError in Posts#index

Showing /home/akash/rails/forum/app/views/posts/index.html.erb where line #41 raised:

请帮我解决这个问题。提前致谢。

希望您已经遵循了所有开始步骤并且 运行 没有迁移 :avatar 没有 :profile_picture 根据您的生成器它将为 :profile_picture 创建附件字段

来自您的代码

validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

应该改为

validates_attachment_content_type :profile_picture, content_type: /\Aimage\/.*\Z/