如何在不继承自 ActiveRecord::Base 的类中使用 Rails 的 Paperclip gem?

发布于 2024-12-22 23:40:11 字数 636 浏览 1 评论 0原文

似乎有些人通过执行以下操作使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-12-29 23:40:11

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

绳情 2024-12-29 23:40:11

我最近必须弄清楚这一点。您需要使用一些 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,但其他配置也可能有效。

class Importer
    extend  ActiveModel::Callbacks
    include ActiveModel::Validations

    include Paperclip::Glue

    define_model_callbacks :save
    define_model_callbacks :destroy

    validate :no_attachement_errors

    attr_accessor :id, :import_file_file_name, :import_file_file_size, :import_file_content_type, :import_file_updated_at

    has_attached_file :import_file,
                      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
                      :url  => "/system/:attachment/:id/:style/:filename"

    def initialize(args = { })
      args.each_pair do |k, v|
        self.send("#{k}=", v)
      end
      @id = self.class.next_id
    end

    def update_attributes(args = { })
      args.each_pair do |k, v|
        self.send("#{k}=", v)
      end
    end

    def save
       run_callbacks :save do
       end
    end

    def destroy
      run_callbacks :destroy do
      end
    end

    # Needed for using form_for Importer::new(), :url => ..... do
    def to_key
      [:importer]
    end

    # Need a differentiating id for each new Importer.
    def self.next_id
      @@id_counter += 1
    end

    # Initialize beginning id to something mildly unique.
    @@id_counter = Time.now.to_i

  end

文件上传的表单可能如下所示:

<%= form_for Importer.new, :url => import_nuts_path do |form| %>
   <%= form.file_field 'import_file' %>
   <%= form.submit 'Upload and Import' %>
<% end %>

NutsController 将执行以下操作:

class NutsController < ActionController::Base
    def import
       importer = Importer.new(params[:importer])
       importer.save
    end
end

注意:: 您必须调用“保存”,否则什么也不会发生。调用“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.

class Importer
    extend  ActiveModel::Callbacks
    include ActiveModel::Validations

    include Paperclip::Glue

    define_model_callbacks :save
    define_model_callbacks :destroy

    validate :no_attachement_errors

    attr_accessor :id, :import_file_file_name, :import_file_file_size, :import_file_content_type, :import_file_updated_at

    has_attached_file :import_file,
                      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
                      :url  => "/system/:attachment/:id/:style/:filename"

    def initialize(args = { })
      args.each_pair do |k, v|
        self.send("#{k}=", v)
      end
      @id = self.class.next_id
    end

    def update_attributes(args = { })
      args.each_pair do |k, v|
        self.send("#{k}=", v)
      end
    end

    def save
       run_callbacks :save do
       end
    end

    def destroy
      run_callbacks :destroy do
      end
    end

    # Needed for using form_for Importer::new(), :url => ..... do
    def to_key
      [:importer]
    end

    # Need a differentiating id for each new Importer.
    def self.next_id
      @@id_counter += 1
    end

    # Initialize beginning id to something mildly unique.
    @@id_counter = Time.now.to_i

  end

A form for a file upload may look like the following:

<%= form_for Importer.new, :url => import_nuts_path do |form| %>
   <%= form.file_field 'import_file' %>
   <%= form.submit 'Upload and Import' %>
<% end %>

and the NutsController would have the following action:

class NutsController < ActionController::Base
    def import
       importer = Importer.new(params[:importer])
       importer.save
    end
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文