【发布时间】:2019-06-05 07:45:52
【问题描述】:
我正在将我的 Rails 应用程序从使用 PaperClip 迁移到 ActiveStorage。
在我的一个模型中,我有以下方法(使用回形针):
class ECard < ActiveRecord
def thumb_url
self.attachment.url(:thumb)
end
end
在控制器中我有:
def by_type
@e_cards = ECard.where(type_id: params[:type_id]).as_json(:only => [:id, :name], :methods => [:thumb_url])
respond_to do |format|
format.json { render json: @e_cards }
end
end
现在,我正在使用 ActiveStorage,如何从 thumb_url 方法中获取附件的缩略图 url?
作品:Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true)
不起作用:Rails.application.routes.url_helpers.rails_blob_path(attachment.variant(resize: '200x200'), only_path: true) 这会引发错误:NoMethodError (undefined method 'signed_id' for #<ActiveStorage::Variant:0x00007fac1960eab0>)
我如何做到这一点?
【问题讨论】:
标签: ruby-on-rails rails-activestorage