【问题标题】:Undefined method "has_attached_file" for my ActiveRecord model我的 ActiveRecord 模型的未定义方法“has_attached_file”
【发布时间】:2023-03-06 14:09:01
【问题描述】:

我完全按照https://github.com/thoughtbot/paperclip 的文档在我的应用程序中安装实现回形针以进行图像上传。我目前正在使用 gem '回形针','~> 5.0.0.beta1'。完成迁移后,四列已正确添加到我的架构中:

t.string   "picture_file_name"
t.string   "picture_content_type"
t.integer  "picture_file_size"
t.datetime "picture_updated_at"

因此,我的回形针应该正确安装。但是,当我继续将以下两行添加到我的模型类中时:

  has_attached_file :picture, styles: { medium: "300*300>", thumb: "100*100" }, default_url: "/images/start_project3.jpg"
  validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/

一切都崩溃了。我尝试在 Rails 控制台中创建、搜索或与模型类相关的任何内容,它对我大喊大叫并出现以下错误:

NoMethodError: undefined method `has_attached_file' for #<Class:0x0055bd71ec0228>

我尝试了多个版本的回形针,从早期版本 4.3.0 到最新版本的回形针,但问题仍然存在。我还在更改和迁移之间重新启动了我的服务器,但这并没有解决问题。这是我执行的迁移:

class AddAttachmentPictureToProjects < ActiveRecord::Migration
  def self.up
    change_table :projects do |t|
      t.attachment :picture
    end
  end

  def self.down
    remove_attachment :projects, :picture
  end
end

我现在完全不知道该怎么做。 这是我的 gem 文件:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.7.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'pry-rails'
gem 'annotate'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'faker'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'pg_search'
gem 'paperclip', '~> 5.0.0.beta1'
# gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
# gem "paperclip", "~> 4.3"

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'faker'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

group :production do
  gem 'newrelic_rpm'
  gem 'rails_12factor' # error feedback
end

【问题讨论】:

    标签: ruby-on-rails ruby paperclip undefined


    【解决方案1】:

    我不确定发生了什么。我最终进行了数据库回滚并再次重新迁移了迁移。然后我退出了在后台运行的所有终端并重新启动了一切。它现在正在工作。

    【讨论】:

    • 当我在$ rails console 中调用带有回形针附件的模型时,我突然开始看到此错误消息; $ rails server 没有问题。我不知道发生了什么,但重新启动终端为我做到了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多