· 1 min read
Screencast: ActiveAttr in eigenen Klassen verwenden
Downloads in verschiedenen Formaten:
Resourcen:
Gemfile
[ruby] gem ‘active_attr’ [/ruby]
models/message.rb
[ruby] class Message include ActiveAttr::Model
attribute :name attribute :email attribute :content attribute :priority # type: Integer, default: 0
attr_accessible :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 end [/ruby]
rails console
[ruby] m = Message.new(priority: 1) m.priority m.priority = 1 m.priority? [/ruby]
controllers/messages_controller.rb
[ruby] def new @message = Message.new end
def create @message = Message.new(params[:message]) if @message.valid? # TODO send message here redirect_to root_url, notice: “Message sent! Thank you for contacting us.” else render “new” end end [/ruby]