2011-01-27 4 views
0

ActiveRecord :: Baseから継承されていないモデルで、Rails 3でpapaerclipを使用したいと思います。他のモデルと同じようにモデルを保存する必要はありません。一部のActiveModelミックスだけが使用されます。ペーパークリップon

class ThemeSettings 
    include ActiveModel::Validations 

    validate :no_attachement_errors 
    has_attached_file :logo, 
        :styles => { :mini => '48x48>', :small => '100x100>', :normal => '240x240>', :large => '600x600>' }, 
        :default_style => :normal, 
        :url => "/assets/logos/:id/:style/:basename.:extension", 
        :path => ":rails_root/public/assets/logos/:id/:style/:basename.:extension" 

    def logo_file_name 
    .. 
    end 
    def logo_file_name=(file_name) 
    .. 
    end 
    def logo_content_type .. 
    def logo_content_type=(content_type) .. 
    def logo_file_size .. 
    def logo_file_size=(file_size) .. 
    def logo_updated_at .. 
    def logo_updated_at=(updated_at) .. 
end 

ペーパークリップがそれを好きではない:

私はこのようなものを作っ NoMethodError: undefined method 'has_attached_file' for ThemeSettings:Classhas_attached_file方法を混入されていません。 Paperclipを単純なクラスのように納得させるにはどうすればいいですか?ご協力いただきありがとうございます!

答えて

4

DBテーブルがない場合でも、ActiveRecord::Baseから継承する方がずっと簡単です。そうすれば他のすべての宝石は完璧に動作します。

class ThemeSettings < ActiveRecord::Base 
    def self.columns 
    @columns ||= []; 
    end 

    def self.column(name, sql_type = nil, default = nil, null = true) 
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, 
     sql_type.to_s, null) 
    end 

    # Override the save method to prevent exceptions. 
    def save(validate = true) 
    validate ? valid? : true 
    end 

    column :logo_file_name 
    column :logo_content_type 
    column :logo_file_size 
    column :logo_updated_at 
    # You can override the = methods here. 

    validate :no_attachement_errors 
    has_attached_file :logo, 
        :styles => { :mini => '48x48>', :small => '100x100>', :normal => '240x240>', :large => '600x600>' }, 
        :default_style => :normal, 
        :url => "/assets/logos/:id/:style/:basename.:extension", 
        :path => ":rails_root/public/assets/logos/:id/:style/:basename.:extension" 


end 
2

はテストされていないが、理論的にはこの作業をする必要があります - これはどう:

require 'paperclip' 

class ThemeSettings 
    include ActiveModel::Validations 
    include Paperclip 

    has_attached_file # ... 

    # ... 
end 

は、ペーパークリップを必要とし、あなたのクラスにモジュールが含まれています。

+0

これは私にとっては役に立たなかった – Tony

関連する問題