The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Wednesday, December 19, 2012

rails :remote => true jQuery events

I constantly find myself looking these up, saving this link for future reference

https://github.com/rails/jquery-ujs/wiki/ajax

contains all of the rails custom jQuery events for remote forms, such as "ajax:success", "ajax:before"; also includes the method signatures.


Sunday, December 16, 2012

Sending email with Rails Mailers, The Random bits...

See the rails guides and railscasts for getting started with Rails Mailers; 
this article includes some additional information on Rails Mailers not included in those articles.

RailsCasts: http://railscasts.com/episodes/206-action-mailer-in-rails-3
Rails Guides: "Action Mailer Basics" on the rails guides - The guide covers all of the common mailer tasks
  • setup and configuration
  • attachments
  • layouts

Random Bits
These examples were created using rails 3.2
Add importance headers

# importance flag on the email
headers['Importance'] = 'high'
headers['X-Priority'] = '1'
headers['X-MSMail-Priority'] = 'High'
NOTE: many email clients will ignore these headers

Add receipt header

# require email receipt
headers['Return-Receipt-To'] = 'confirm@example.com'
NOTE: many email clients will ignore these headers

Testing mailer headers with rspec

If you have rspec-rails gem installed with your rails application then running the generator will stub out most of your mailer tests, including a fixture file for the mailer view, which you may or may not want to remove.

mail.header['X-MSMail-Priority'].value.should == 'High'
NOTE: test against mail.header, not mail.headers

Use a different layout for some Mailer actions

our default layout is the customer facing one but we want to override it in some cases

class ExampleMailer < ActionMailer::Base
default from: "from@example.com"
# default layout for ExampleMailer
layout "customer_email"
# the order ready is sent to the customer and uses the default layout - customer_email
def order_ready
@greeting = "Hi"
mail to: "to@example.org"
end
# the ready to ship email is internal only - use the internal layout
def ready_to_ship
@greeting = "Hi Hi"
mail(to: "internal@example.org") do |format|
format.html { render :layout => "internal_email" }
format.text { render :layout => "internal_email" }
end
end
end