Using Rails’ Flash Messages with AJAX Requests

Have you ever wondered how to get access to the Ruby on Rails‘ flash message when performing a AJAX or restful web request?  You might hit yourself on the head when you discover how easy it is.  Simply append the flash message to the Response headers.  You could even wrap this in a helper, and using an after_filter to automatically add the header for you on every AJAX response.

class ApplicationController < ActionController::Base
after_filter :flash_headers

def flash_headers
  # This will discontinue execution if Rails detects that the request is not
  # from an AJAX request, i.e. the header wont be added for normal requests
  return unless request.xhr?

  # Add the appropriate flash messages to the header, add or remove as
  # needed, but I think you'll get the point
  response.headers['x-flash'] = flash[:error]  unless flash[:error].blank?
  response.headers['x-flash'] = flash[:notice]  unless flash[:notice].blank?
  response.headers['x-flash'] = flash[:warning]  unless flash[:warning].blank?

  # Stops the flash appearing when you next refresh the page
  flash.discard
end

And then you just read the header with whatever you happen to be reading it with. For completeness sake here is an example of how to read the header in JavaScript using Prototype:

 new Ajax.Request('/your/url', {
  onSuccess: function(response) {
    var flash = response.getHeader('x-flash);
    if (flash) alert(flash);
 }
});

How To Install Phusion Passenger on a CentOS/cPanel Box

The Usual Disclaimer
Please beware that even if these instructions work, that they may break your setup.  While these worked fine for me, you should take extreme care applying them to your situation.  Follow these instructions at your own risk. They work fine for me on a CentOS 4.6 box with cPanel 11.23.4-R26138 and WHM 11.23.2.

First off, Phusion Passenger only works with Apache 2.x so if your server doesn’t have that thats the next thing you’ll have to do.  T o upgrade Apache, go to your main control panel at https://server.ip.address:2087/ and click the “Apache Update” link on the left.  If given the option, I highly recommend that you choose Apache 2.2 instead of 2.0.

Next, download Passenger. Assuming you already have Ruby and RubyGems installed on your server, simply run (as root) gem install passenger. This will pull the passenger library and code down to the server and place it with your other RubyGems.  It’s important to know that this does not install Passenger into Apache, so obviously its non-operational.  If you don’t have Ruby and RubyGems, you’re way too far down the line with reading this article and need to get up to speed with actually getting those on to your box.  Google can help with that!

At this point it’s probably useful to have the Passenger User Guide up on screen, just for reference.

Third, you need to compile and install the Passenger module within Apache. This sounds worse than it is, but before you do it, it is important to set a couple of environment variables to make it work properly.  In your SSH console type thrse two commands into it:

export APXS2=/usr/local/apache/bin/apxs
export APR_CONFIG=/usr/local/apache/bin/apr-1-config

Next, run:

passenger-install-apache2-module

It might take a few minutes, but if everything goes well you’ll eventually end up with Passenger telling you to add a few lines to your Apache configuration file (the code you get may differ slightly):

LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.2
PassengerRuby /usr/local/bin/ruby

However, despite what it tells you, do not put it in the httpd.conf file – cPanel can rewrite that file and your changes can be lost breaking your passenger configuration and virtual site.  Instead, add it to /usr/local/apache/conf/includes/pre_virtualhost_global.conf – this file might not exist until you make it, but that’s okay.

Lastly restart Apache and if you didnt get any critial errors, you should be in business.  I have had issue on other servers where the Rails app failed to load because the RubyGems version was too old, so keep your eye on the error_log file if Apache starts but it still doesn’t work.  Also refer to the Passenger User Guide for further configuration and usage information.

Solve uninitialized constant ApplicationController in Rails 2.3

So I am casually writing my Ruby on Rails application, like I have a million times before on the newest version of Rails (which at time of writing, was version 2.3).  So I open up an blank project with some code I use in all my Rails apps, and copy over the relevant files, and replace the application_crontroller.rb with application.rb.

But as it turns out, there were some major changes with 2.3, not the least being that the application controller is no longer called application.rb Now it’s referred to as application_controller.rb.

So it’s easy to fix, just rename the file or, it you’re into scripty goodness, run:

rake rails:update

Be sure to update your server stack before deploying. Obviously if you rename the file to get it working on Rails 2.3 and upload to a Rails 2.2 server you’ll be in trouble. Don’t forget that Rails 2.3 also requires a new version of Phusion Passenger!