如何在不继承自 ActiveRecord::Base 的类中使用 Rails 的 Paperclip gem?
似乎有些人通过执行以下操作使 Paperclip 在常规 ruby 类上工作:
require "paperclip"
Class Person
include Paperclip
has_attached_file :avatar,{}
end
请参阅 这里 即使使用主要的 Paperclip 存储库,这对我来说也不起作用:
$ bundle exec rails c
>> Rails.version
=> "3.1.3"
>> require 'paperclip'
=> false
>> class Monkey
>> include Paperclip
>> has_attached_file :avatar,{}
>> end
NoMethodError: undefined method `has_attached_file' for Monkey:Class
有没有人得到这个工作并且可能给出可能出现问题的线索?
谢谢!
There seems to be some people that have gotten Paperclip working on a regular ruby class by doing something like the following:
require "paperclip"
Class Person
include Paperclip
has_attached_file :avatar,{}
end
See here
This does not work for me even when using the main Paperclip repo:
$ bundle exec rails c
>> Rails.version
=> "3.1.3"
>> require 'paperclip'
=> false
>> class Monkey
>> include Paperclip
>> has_attached_file :avatar,{}
>> end
NoMethodError: undefined method `has_attached_file' for Monkey:Class
Has anyone gotten this working and can possibly give a clue on what could be going wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Paperclip 非常明确地适用于 AR。
另一种选择是使用载波,它在 AR 之外运行得很好,可以使用各种 ORMS,也可以不使用:
https:/ /github.com/jnicklas/carrierwave
Paperclip is pretty explicitly for use with AR.
Another option is to use carrier wave instead which works pretty well outside AR, with a variety of ORMS, or none:
https://github.com/jnicklas/carrierwave
我最近必须弄清楚这一点。您需要使用一些 ActiveModel 内容来定义您的库类或模型。具体来说,要使用Paperclip,您需要以下方法:save、destroy、它们的回调、to_key(与form_for一起使用)、id的attr_acessors,当然还有每个附加文件的*_file_name、*_file_size、*_content_type、*_updated_at 。
下面的课程应该为您提供所需的最低限度的实现。此“解决方案”自 2012 年 9 月 10 日起使用 Rails 3.2.8、Ruby 1.9.3 和 Paperclip 3.2.0,但其他配置也可能有效。
文件上传的表单可能如下所示:
NutsController 将执行以下操作:
注意:: 您必须调用“保存”,否则什么也不会发生。调用“destroy”将会删除服务器端的文件。由于此类不是持久性的,因此您可能应该在控制器中完成后执行此操作,否则如果不进行任何清理,最终将导致文件空间泄漏。
安全说明:此“解决方案”没有对 new() 和 update_attributes() 使用任何 ActiveModel::MassAssignmentSecurity,但此类实际上并不需要它。您的里程可能会有所不同。出去要小心!
干杯,
-博士。极性
I recently had to figure this out. You need to use some ActiveModel stuff in defining your library class or model. Specifically, to use Paperclip, you need the following methods: save, destroy, their callbacks, to_key (for use with form_for), attr_acessors for id, and of course, *_file_name, *_file_size, *_content_type, *_updated_at for each attached file.
The following class should give you the minimum implementation you need. This "solution" uses Rails 3.2.8, Ruby 1.9.3, and Paperclip 3.2.0 as of Sept 10, 2012, although other configurations may work.
A form for a file upload may look like the following:
and the NutsController would have the following action:
Note:: You have to call "save" or nothing will happen. Calling "destroy" will delete the file on the server side. Since this class is not persistent, you probably should do that after you are done with it in the controller, or you'll end up with a file space leak, if you don't do any clean up.
Security Note: This "solution" is not using any ActiveModel::MassAssignmentSecurity for new() and update_attributes(), but this class doesn't really need it. Your mileage may vary. Be careful out there!
Cheers,
-Dr. Polar