Validating attachment contents on upload with Paperclip gem

半世苍凉 提交于 2019-12-05 04:57:55
Tim Smith

Ah, but you can access the File object from a validator before the record is saved, by calling [attachment].to_file. Running my parser against [attachment].to_file.path (instead of attachment.path) in my_method seems to work just fine.

Per https://stackoverflow.com/questions/7047183/how-to-set-an-attribute-for-a-model-instance-in-a-paperclip-post-process-callback, [attachment].queued_for_write[:original].path might also be an option.

Great question, and perhaps someone who has experience with this can give you a better answer, but given that there doesn't appear to be a way to do this within the DSL provided by Paperclip, I would recommend just building your own Paperclip validator (not post-processor).

You can use an existing validator as an example, and drop it into app/lib/paperclip/matchers/validate_foobar.rb (be sure to set your config/application.rb to load from your lib folder) and you're good to go.

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