Non-ActiveRecord Objects에 validate

1312 단어 ActiveRecordF#HTML
자세히 보기
Active Record 대상이 아닌Validation에 대해 간단한include Active Record::Validations는 모듈을 써서lib 아래에 놓고validateable를 만들어야 합니다.rb
module Validateable
  [:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
  def method_missing(symbol, *params)
    if(symbol.to_s =~ /(.*)_before_type_cast$/)
      send($1)
    end
  end
  def self.append_features(base)
    super
    base.send(:include, ActiveRecord::Validations)
  end
end

그리고 모델 밑에 있어요.
class Logo < ActiveRecord::Base
  include Validateable
  attr_accessor :size

  validates_presence_of :size
  validates_presence_of :name
  
  file_column :photo, :magick => { 
    :versions => { "thumb" => "235x90>", "medium" => "640x480>" }
  }
end

페이지

 {:action => :create},:html => {:multipart => true} do |f| %>
  name:
size:

마지막 공백 제출, 오류 보고
There were problems with the following fields:
  • Name can't be blank
  • Size can't be blank

  •  

    좋은 웹페이지 즐겨찾기