簡體   English   中英

如何使用來自同一Rails模型上兩個不同寶石的具有相同“名稱”的兩種不同方法?

[英]How to use two different method with the same “name” from two different gems on the same rails model?

我正在使用的應用程序(導軌4.2.7)同時使用了carrierwavepaperclip來上傳同一數據模型User (以下方案)上兩個不同字段的圖像。

create_table "users", force: :cascade do |t|
  t.string   "email"
  t.string   "first_name",             limit: 255
  t.string   "last_name",              limit: 255
  t.string   "avatar_file_name",       limit: 255
  t.string   "avatar_content_type",    limit: 255
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.string   "cv_file"
end

avatar場是一個回形針對象和cv_filecarrierwave上傳。

現在,對於cv_file字段的后台處理,我正在使用carrierwave_backgrounder gem,對於avatar字段,我使用的是delay_paperclip gem。

這兩個寶石都暴露了process_in_background來處理將圖像上傳到背景。 所以我的用戶模型看起來像:

class User < ActiveRecord::Base
  # carrierwave
  mount_uploader :cv_file, CvFileUploader
  process_in_background :cv_file

  # paperclip
  has_attached_file :avatar, 
                    :default_url => "default-avatar.png",
                    :styles => {
                      :thumb => ['100x100#', :jpg, :quality => 80]
                    }
  process_in_background :avatar, processing_image_url: "default-avatar.png"

  # ...

end

嘗試訪問應用程序上的任何頁面時出現此錯誤。

未定義的方法“ remove_avatar?” 對於

你的意思是? remove_cv_file?

任何幫助將不勝感激。 謝謝!

簡單地分叉DelayedPaperclip並更改名稱可能是值得的。 除此之外,您可能會嘗試擴展DelayedPaperclip並對方法進行別名:

module AliasDelayedPaperclip extend DelayedPaperclip alias_method :paperclip_process_in_background, :process_in_background end

我尚未對此進行測試,它可能需要進行一些修改才能真正起作用,但這至少是您可以嘗試的一種建議。

暫無
暫無

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

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