mri*_*ula 2 ruby-on-rails rails-activestorage
我正在将我的 Rails 应用程序从使用 PaperClip 迁移到 ActiveStorage。
在我的一个模型中,我有以下方法(使用回形针):
class ECard < ActiveRecord
def thumb_url
self.attachment.url(:thumb)
end
end
Run Code Online (Sandbox Code Playgroud)
在控制器中,我有:
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
Run Code Online (Sandbox Code Playgroud)
现在,我正在使用 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>)
我如何实现这一目标?
找到了!
def thumb_url
Rails.application.routes.url_helpers.rails_representation_url(attachment.variant(resize: "200x200").processed, only_path: true)
end
Run Code Online (Sandbox Code Playgroud)
从这个答案中找到。
| 归档时间: |
|
| 查看次数: |
2426 次 |
| 最近记录: |