error_messages_for you

December 24, 2006

Making pretty error messages is easy. And fun! I overwrote error_messages_for because I wanted to include multiple objects in the error message list. Then I freaked out and loaded it up with lotsa functionality.

  • Print error messages for multiple objects by listing them in an array. Objects can be specified:
    • The object. (@user or non_instance_object)
    • Specify objects as a string representing the name of an instance variable ('email_address' to refer to @email_address)

<%= error_messages_for [@user, 'email_address', non_instance_object] %>

<%= error_messages_for 'non_instance_object' %>

  • Customize the header message or the sub header message

<%= error_messages_for 'user', :header_message=>'%d errors prohibited you from creating a happy pony party.', :header_sub_message=> 'these ponies were bad:' -%>

  • Skip or customize the field name display. Perhaps in one context you call it “Primary email address” and in another, you refer to it as “Contact Information”. Or you just dont want to report errors on the field.

<%= error_messages_for 'user', :sub=>{'email_address_text'=>'Primary Email Address'}, :skip=>['credit_card_number'] -%>

  • Compatible with the awesome globalize plugin. (If you don’t like global things, then you should remove the .t business.

Codeage:
Read the rest of this entry »


RoR Email Sending Performance

December 15, 2006

If you need to send a lot of emails from Ruby on Rails you may run into problems. Out of the box I was getting about 1 email/second through smtp from our low end server.

There’s an easy fix however!

put this in your environment.rb

ActionMailer::Base.delivery_method = :thread_smtp
module ActionMailer
class Base
def perform_delivery_thread_smtp(mail)
thread = Thread.new do
perform_delivery_smtp(mail)
end
thread.run
end
end
end

Doing this I was able to get smtp up to 13 emails/second with ruby’s smtp (there were a few DB calls as well for each email send). We use msmtp to send secure email, this has a similar configuration change. Using that I got up to 16 emails/second. I guess most things are faster than ruby.

Please do not use this technology for creating a spam engine, world domination, or evil.