Implicit multipart emails using Ruby on Rails and ActionMailer won’t work

June 26th, 2009 by Justin Ball

I've been beating my head against the wall trying to figure out why html emails from my Rails Engine are sent out as plain text ie the email shows up with all the html tags visible. Turns out there is an issue in Rails 2.3.2:

https://rails.lighthouseapp.com/projects/8994/tickets/2263-rails-232-breaks-implicit-multipart-actionmailer-tests. For now I'm just setting my emails to html, but I'm hoping to find either a monkey patch or a Rails update that fixes the issue.

Tags:   ·

No Comments.

Sending email with Ruby on Rails 2.3.2 and gmail

June 25th, 2009 by Justin Ball

If you need an easy way to setup email for your company or for a client it's hard to not love Google Apps. (The 'standard' ie free version is here).

It is very likely that at some point you will need to send out emails from your application. Don't cry. Gmail can do this for you. Rails 2.3.2 makes it easier than ever with the addition of 'enable_starttls_auto'. Put the following code in production.rb, development.rb or environment.rb and you 'should' be able to send out emails:

 
  ActionMailer::Base.smtp_settings = {
    :enable_starttls_auto => true,
    :address        => 'smtp.gmail.com',
    :port           => 587,
    :domain         => 'your.domain.com',
    :authentication => :plain,
    :user_name      => 'login@your.domain.com',
    :password       => 'some_password'
  }
 

If you are one of the lucky ones this will actually work. It didn't work for me but since I still needed to get email working here's what I did:

First be sure to activate the email account that you are using. To do that you only need to login. Google will walk you through entering a captcha etc.

You might get this error:
Net::SMTPAuthenticationError: 530 5.7.0 Must issue a STARTTLS command first.

If you do then check your Ruby version. Ruby 1.8.7 has this built in but Ruby 1.8.6 does not.

I'm currently running my apps on EngineYard's Solo product <a href="https://cloud-support.engineyard.com/discussions/problems/203-engineyard-cloud-support-for-ruby-187-p73">which is currently using Ruby 1.8.6 so changing versions isn't an option (not an easy option anyway).

The next best thing is to get action_mailer_tls. Most of the links you will find in forum and blog posts are broken. The code is on github and neatly packaged as a gem: http://github.com/openrain/action_mailer_tls/tree/master. Follow the directions to install and configure the plugin there and you should be able to send email.

Here's a couple of handy conversations for reference:
http://www.railsforum.com/viewtopic.php?id=28480
http://www.ruby-forum.com/topic/184137

Tags:   · · · ·

1 Comment

jeweler and the empty gemspec

June 24th, 2009 by Justin Ball

I've been racking my brain trying to figure out why my gemspec for a new gem had no files in it. I'm using jeweler to build the gem and it's worked great in the past. Then I read in the docs that jeweler will include any files in the gemspec that are not in .gitignore and it occurs to me that jeweler is getting the list of files from git. That means you have to do 'git init' and 'git add .' in order to have any files in your project.

So the next time you are building a gem and you do this:

 
    gemspec.files.include %w(
                            tasks/*
                            db/migrate/*.rb
                            app/**/**/**/*
                            config/*
                            locales/*
                            rails/*
                            test/*
                            lib/**/*
                            public/**/* )
 

and you get this:

 
undefined method `include' for []:Array

Just know that your gemspec doesn't contain any files and you don't actually need the include listed above in your Rakefile. Instead you just need to add all your files to git.

Tags:   · ·

No Comments.

New Uploader Gem

June 20th, 2009 by Justin Ball

I just pushed out a new uploader gem. Get the new code from github or install the gem with sudo gem install uploader. This comes with a few more translations and a fix for a nasty bug. The format was not properly set for uploads via swfupload. Unfortunately, even without the format it worked on uploads from my laptop, but not really for anyone else. I really hate that. At any rate it's fixed now.

Tags:   · ·

No Comments.

Testing Rails Engine Gems

June 16th, 2009 by Justin Ball

I've been working on a number of gems that are basically packaged Ruby on Rails engine plugins. It turns out that turning gems into plugins is pretty easy to do. However, testing them can be a pain. Here are a few things I came up with.

After looking at how Clearance handles tests I've decided that it's ok to embed a basic Rails application in your test directory.

The next trick is getting the gem you are working on to load in your embedded Rails application. The Clearance guys added a file in config/initializers that looks like this:

 
# This simulates loading the clearance gem, but without relying on
# vendor/gems
 
clearance_path = File.join(File.dirname(__FILE__), *%w(.. .. .. ..))
clearance_lib_path = File.join(clearance_path, "lib")
 
$LOAD_PATH.unshift(clearance_lib_path)
load File.join(clearance_path, 'rails', 'init.rb')
 

I thought that was brilliant but it didn't work for me. After messing around in the Rails code for a bit I found a bit of helpful documentation on 'plugin_locators':


The classes that handle finding the desired plugins that you‘d like to load for your application. By default it is the Rails::Plugin::FileSystemLocator which finds plugins to load in vendor/plugins. You can hook into gem location by subclassing Rails::Plugin::Locator and adding it onto the list of plugin_locators.

That sounded like the perfect solution to my problem. I figured I would simply add a plugin locator that pointed to the root of the gem I was building. Turns out that works great. In the embedded rails application inside of config/environment.rb I added this code:

 
class TestGemLocator < Rails::Plugin::Locator
  def plugins
    Rails::Plugin.new(File.join(File.dirname(__FILE__), *%w(.. .. ..)))
  end
end
 
Rails::Initializer.run do |config|
  config.time_zone = 'UTC'
  config.plugin_locators << TestGemLocator
end
 

Now my tests load correctly and all is well. Context is important. If you want to have a look at the full project I put it into a gem called disguise which you can get from github.

Tags:   · ·

No Comments.

A copy of ApplicationController has been removed from the module tree but is still active! And can’t dup NilClass

June 13th, 2009 by Justin Ball

If you've been working on Rails 2.3 engines (the new stuff that comes with Rails 2.3 not the old engine plugin) and you start running into either of these errors:

A copy of ApplicationController has been removed from the module tree but is still active!

Or

can’t dup NilClass

Don't go find a new job at McDonald's. Read this article and feel the love again.

In fact if you do any engine or plugin or gem development read that article. It will make you happy again.

Tags:   ·

No Comments.

Disguise your Rails Application

June 12th, 2009 by Justin Ball

I spent quite a while trying to figure out how to theme my Ruby on Rails applications. Turns out after you build a piece of software people want to use it to do other stuff. Go figure.

I really like how simple it is to skin a Wordpress site so I stole borrowed some ideas.

From all of my research and effort and late night I give you disguise the ruby gem that makes it simple to skin your Rails application and impress everyone (ok maybe your mom).

Disguise makes it possible to generate a theme for your Rails website, set the current 'theme' using an admin interface or change the theme based on the current url.

Install the gem:

 
  sudo gem install disguise
 

or get the source code at:
http://github.com/jbasdf/disguise/tree/master

Tags:   · · ·

No Comments.

Stupid WTF! ActionView::MissingTemplate Exception: Missing template users/_user.erb

June 5th, 2009 by Justin Ball

If you've spent much time working with Ruby on Rails and more especially if you've done anything with json you might have run across errors like this:

ActionView::MissingTemplate Exception: Missing template users/_user.erb

I was having this problem and doing a lot of cursing which is common when I stay up and write code until 2am which I know I should do and I also know that I shouldn't write really long run on sentences but I do it anyway.

This error is usually the result of some code that looks like this:

 
format.json do
  @user_html = render_to_string( :partial => 'users/user', <img src='http://www.justinball.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject => @user )
  render :json => { :success => true,
                              :user_html => @user_html  }
end
 

The cause of this error is that the format of the current request is :json. There isn't an _user.json.erb template so Rails tries to find an _user.erb file. That doesn't exist either and boom you spend the night sounding like a sailor.

Now I don't pretend to be an expert but I have been accused of being a hack. I solve the problem by changing the template format:

 
format.json do
  @template.template_format = :html
  @user_html = render_to_string( :partial => 'users/user', <img src='http://www.justinball.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> bject => @user )
  render :json => { :success => true,
                              :user_html => @user_html  }
end
 

The key is the addition of:

 
  @template.template_format = :html
 

Don't yell at me if it breaks. I'm open to better suggestions but for now this does work and now my I can find other reasons to stay up late.

Tags:   · · · ·

No Comments.

newgem

May 30th, 2009 by Justin Ball

I've been using newgem to build Babelphish (translation helper thing). The docs from the ruby forge site are a bit out of date. If you use newgem to build anything be sure to look at this post from Dr Nic and pay attention to the options you can pass in. In particular I noticed that the instructions on the rubyforge site indicate the gem will build a default website. If you want to do that you need to pass in the -w option.

Tags:   · · ·

No Comments.

Ruby gems and “Couldn’t get release_id, upload failed?”

May 30th, 2009 by Justin Ball

I've been experimenting with Ruby gems over the past few weeks. I use a couple of other gems to make the process easier including rubyforge, jeweler and newgem.

Jeweler has a rake task 'rubyforge:release' that uploads your gem for you. The problem was that it would timeout and give me "Couldn't get release_id, upload failed?". I assumed that I had configured something wrong so I tried to upload the file directly to rubyforge. That didn't work either so I assumed (wrongly) that my project had something wrong with it. I was about to give up when I decided to check my gem size. It was 100MB!

Somehow I was packaging the gem inside itself on each release and it had grown into a beast. So if your release is timing out check your gem size and make sure it isn't going nuts.

Tags:  

No Comments.