簡體   English   中英

當我使用回形針遷移錯誤發生

[英]when I use paperclip migration error occur

運行此命令 => rails generate paperclip photo image

創建遷移文件

class AddAttachmentImageToPhotos < ActiveRecord::Migration[5.1]

  def self.up
    change_table :photos do |t|

      t.attachment :image
    end
  end

  def self.down
    remove_attachment :photos, :image
  end
end

遷移錯誤

rails db:migrate
== 20210223102018 AddAttachmentImageToPhotos: migrating =======================
-- change_table(:photos)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (given 3, expected 2)
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:4:in `block in up'
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:3:in `up'
/home/rizwan/projects/ClipUploader/bin/rails:5:in `<top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:10:in `block in <top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:7:in `<top (required)>'

Caused by:
ArgumentError: wrong number of arguments (given 3, expected 2)
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:4:in `block in up'
/home/rizwan/projects/ClipUploader/db/migrate/20210223102018_add_attachment_image_to_photos.rb:3:in `up'
/home/rizwan/projects/ClipUploader/bin/rails:5:in `<top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:10:in `block in <top (required)>'
/home/rizwan/projects/ClipUploader/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我有同樣的問題,我通過如下更改遷移解決了它,

class AddAttachmentImageToPhotos < ActiveRecord::Migration[5.1]

  def self.up
    add_attachment :photos, :image
  end

  def self.down
    remove_attachment :photos, :image
  end
end

當使用使用 PaperClip 的 Ruby 3 從 Rails 5 升級到 6.1 時,我遇到了類似的問題。 不確定是什么導致了問題,但像正常遷移一樣添加列會有所幫助。

  class AddLogoColumnsToUsers < ActiveRecord::Migration[6.1]
    def up
       add_column :users, :logo_file_name, :string
       add_column :users, :logo_file_size, :integer
       add_column :users, :logo_content_type, :string
       add_column :users, :logo_updated_at, :datetime
    end

    def down
       remove_column :users, :logo_file_name, :string
       remove_column :users, :logo_file_size, :integer
       remove_column :users, :logo_content_type, :string
       remove_column :users, :logo_updated_at, :datetime
    end
  end

您可以使用https://github.com/kreeti/kt-paperclip來存儲圖像。 非常適合我。 我也陷入了同樣的錯誤,但現在工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM