Freezing Rails

Posted by John
on Tuesday, 18 December 2007

Sometimes with remote server's you may have to supply the files relating to the current version of rails your writing for in order to run your app on someone else's server, like mosso.com.

Freezing Rails

So in Terminal, while in your Rails apps directory run:

rake rails:freeze:gems

This will unpack the rails version your using into the /vendor/rails directory of your app.

From there you can upload it to your server and it should just work.

Thawing Out

To reverse the process...

rake rails:unfreeze

This removes the frozen version from your vendor/rails directory.

Picking Up Changes

Also note new rails versions come with config file changes, to pick these up run:

rake rails:update:configs

Freezing to the Edge

You can also freeze (or grab a local development copy) of rails via...

rake rails:freeze:edge

Or Freeze to a specific release with...

rake rails:freeze:edge TAG=rel_2-0-2

This freezes to Rails 2.0.2

Also for...

You can also do this for individual gems, by first grabbing the gemsonrails gem...

sudo gem install gemsonrails

Then moving to your Rails app directory and running...

gemsonrails

This installs some extra RAKE tasks into your app, so now you can freeze a local copy of say the RedCloth gem into your app via...

rake gems:freeze GEM=RedCloth

Rails => Forgot your Password code...

Posted by John
on Wednesday, 07 November 2007

Finally got around to adding a 'forgot your password' feature to AtomPad.com.

For the rest of you, I found an excellent example online at...

I changed the Find_By to the following as Mongrel doesn't seem to like dynamic finds,

In forgot password method...

if @user = User.find(:first, :conditions => [ "email = ?", params[:email] ])

And reset password method...

@user = User.find(:first, :conditions => [ "pw_reset_code = ?", params[:id] ] ) rescue nil

Nice to have that problem out of the way,

Quick and Dirty Ruby Emailer

Posted by John
on Sunday, 04 November 2007

By no means perfect, here's a tiny slice of ruby-on-rails code to send emails...

Net::SMTP.start('localhost') do |smtp|
smtp.send_message 'hello...', 'yourdomain.com', @user.email
end

Remember to setup your email server Postfix, which this guide will help you out.

More to come later, with a proper class-based emailer; waiting for my MX records to propogate...

How-To: Multiple Ruby Apps on the Same Box

Posted by John
on Friday, 24 August 2007

So you’ve deployed your first app to your nice lil’ Linux box via Capistrano, wahey!!

Now presuming you don’t really want to dedicate each server to one app alone, how would you get more than one app on there?

Here’s how,

Apache 2 .Conf

remote onto your server and edit the apache2 .conf file

/usr/local/apache2/conf/httpd.conf

  • look for a line saying ServerName
  • append the server’s IP address to the end of this
  • un-comment it

now when you try restarting apache remotely it’ll know where it is.

next check down the bottom of the file for:

  • Listen 80
  • Include conf/apps/
  • NameVirtualHost * :80

this tells Apache which port to listen to for incoming http requests and to load the config files in the conf/apps/ dir on startup.

Editing your Apps Deploy.rb

Now before you deploy your second app to the new server you must make sure it’ll use a different proxy port. Now by default your first app’l have used 8000 and 8001 so the obvious choice is to use 9000 and 9001.

So, navigate to your second ruby apps /config/ dir and edit it’s deploy.rb file

secondapp/config/deploy.rb

  • first off, you can copy & paste bits from your first apps deploy.rb to make your second, just remember to change the svn repository location to default (as you’ll make your own) and change your apps name.
  • look for => set :apache_proxy_port, 8000 and change it to 9000

deploy your 2nd app

in your second apps dir using the capistrano + deprec gems,

  • cap deprec_setup
  • cap setup_scm <= this’ll give you a new svn location for your app, add it to your deploy.rb file
  • cap deploy_with_migrations
  • cap restart_apache

afterwards you should have 2 ruby apps running on the same server!

If you get problems…

Now life isn’t perfect so chances are you’re gonna run into problems, so here’s some common commands and gotcha’s to help you,

Mongrel Complains .PID files already exist!

  • …ssh in and goto /var/www/apps/secondapp/current/tmp/ and run: rm *.pid

How Do I Re-Start Mongrel?

  • …navigate to app root & run: cap restart_mongrel_cluster

Second App keeps pointing to first, ARGH!!!

  • …ssh in and goto /usr/local/apache2/conf/apps/
  • …run: nano secondapp.conf
  • …make sure <VirtualHost *:80> and it’s balancemember is using 9000 and 9001

Restart Server?

  • …ssh in and run shutdown -r now

Restart Mongrel + Apache?

  • …navigate to first app and run: cap restart_mongrel_cluster
  • …goto second app my run: cap restart_mongrel_cluster
  • …run: cap restart_apache

If you still get problems drop me a line and i’ll try to help,

Best of luck,

John.

Accessing the Ruby RI dictionary

Posted by John
on Friday, 10 August 2007

One of the major plus factors with Ruby and also all the extra gems is that they usually come with helpful documentation, both the RI and RDOC packages.

To access these is pretty simple, if a little daunting at first, from the Terminal.

ri Class

ri Class::method (:: for class mathods)

ri Class#method (# for instance methods)

The first line will return the RI dictionary definition and usage of the [class] you require, the second can be used to drill this down to the related [method] for the [class]

To use this, replace [class] & [method] with something like..

ri Enumerable

ri Enumerable#zip

…here the top line will return the definition & usage of the Enumerable class, the next will drill that down to a particular instance method.

You can also output this content to an HTML file via,

ri -Tf html String#gsub > gsub.html

…or to the Terminal using ANSI formatting

ri -Tf ansi String#gsub

…took this off a part of Why’s Poignant Guide to Ruby, hope to embelish this a bit further later.

John.