pass file to paperclip on back end [rails]

落爺英雄遲暮 提交于 2019-12-06 06:27:51

Ordinarily an uploaded file is passed to your controller as a File object in the params hash, which is then handed by the constructor, by way of attributes=, to the setter method created by Paperclip's has_attached_file--in your model's case Asset#asset= (might want to clarify those names a bit).

No, that's not an answer to your question, but it leads us to the answer. Knowing that you might realize that you can call that setter any time you want with a File as the parameter. E.g.:

class SomeController < ActionController::Base
  def some_action
    @some_asset = Asset.find 123 # (for example)

    file_path = '/tmp/path/to/your/file'
    file      = File.open(file_path, 'r')

    @some_asset.asset = file
    @some_asset.save
  end
end

Hope that's helpful!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!