· 1 min read
Screencast: Rails 3 Active Model
Download:
Download(11.7 MB, 6:47) Alternativer Download für iPod & Apple TV(10.9 MB, 6:47)
Resourcen:
Quellcode:
[ruby] # models/message.rb class Message include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :name, :email, :content validates_presence_of :name validates_format_of :email, :with => /^[-a-z0-9_+.]+@([-a-z0-9]+.)+[a-z0-9]{2,4}$/i validates_length_of :content, :maximum => 500 def initialize(attributes = {}) attributes.each do |name, value| send(”#{name}=”, value) end end def persisted? false end end
# messages_controller.rb def create @message = Message.new(params[:message]) if @message.valid? # TODO send message here flash[:notice] = “Message sent! Thank you for conacting us.” redirect_to root_url else render :action => ‘new’ end end [/ruby]