Mark Kirby

Posts Tagged ‘ruby-on-rails’

Subversion and Ruby on Rails

Friday, February 22nd, 2008

When using Subversion with Ruby on Rails there are a couple of things to take note of.

Firstly, there are files you want to exclude from the Subversion repository, the log files, temporary files and database file. The log files and tmp files can get very large, and the database file might be different on different machines.

To exclude them you can use the script from railsonwave, also attached below. Run this when you set the project up, or at any time afterwards.

#!/bin/sh
svn remove log/*
svn commit -m"removing log files"
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m 'Ignoring all files in /log/ ending in .log'
svn move config/database.yml config/database.example
svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m 'Ignoring database.yml'
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "ignore tmp/ content from now"
svn propset svn:ignore ".htaccess" config/
svn update config/
svn commit -m 'Ignoring .htaccess'
svn propset svn:ignore "dispatch.fcgi" config/
svn update config/
svn commit -m 'Ignoring dispatch.fcgi'

Random mutterings has taken this a step further with this script which will run all this for you when you create a new project.

Secondly, you will want to automatically add new files created by the generate script when you commit. If you use the Subversion command

svn --force add .

To do this, you will end up adding all those files you excluded. Instead try this tip from the Rails Wiki

svn status | grep “^\?” | awk “{print $2}” | xargs svn add

Run that just before a commit and you can be sure all files, old and new have been updated.

An even better solution from the Rails Wiki also is this script which you can save in your apps folder under script as rails-commit.

#!/usr/bin/env ruby

# my take on a easy commit script for rails...
# it is far from prefect, so:
# please feed back the modifications you made to it!
#
# Fixed bugs on empty to_ arrays and spelling mistakes
# by Vesa-Pekka Palmu
# orginal by Cies Breijs -- cies.breijsATgmailDOTcom
# based on a bash script by Anonymous Gentleman
# found here: <a href="http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion">http://wiki.rubyonrails.org/rails/pages/HowtoUseRailsWithSubversion</a>

to_add = []
to_remove = []
to_checkin = []

`svn status`.each_line do |l|
  action_char, path = l.split(' ', 2)
  path.strip!
  case action_char
    when '?'
      to_add << path
    when '!'
      to_remove << path
    when 'M'
      to_checkin << path
  end
end

puts "\nyou are about to..." 

def print_list(array, str)
  puts "\n#{str}:"
  array.each { |i| puts "\t"+i }
  puts "\t<nothing>" if array.length == 0
end

print_list(to_add, 'add')
print_list(to_remove, 'remove')
print_list(to_checkin, 'checkin')

puts "\nplease write something for the commit log and hit enter..."
puts "(hitting enter on an empty line will cancel this commit)\n\n" 

log = gets.strip

if log.empty?
  puts "commit cancelled!\n"
  exit
end

puts "\ncontacting repository...\n" 

`svn add #{to_add.join(' ')}` unless to_add.empty?
`svn remove #{to_remove.join(' ')}` unless to_remove.empty?
puts "\n" + `svn commit -m "#{log.gsub('"', '\"')}"` + "\n" 

puts "running 'svn update' to be sure we are up-to-date..."
puts `svn update`

puts "\nfinished.\n"

Once added, in command line do

ruby script/rails-commit

and the script will run, telling you what will be added, and prompt you to enter a message. Perfect!

How to downgrade Ruby on Rails

Tuesday, February 19th, 2008

I’m learning to use Ruby on Rails at the moment on weekends and after work for a project I’m working on for a friend. The problem is, I’m learning using the book Agile Web Development for Rails, which is written for Rails version 1, and I’ve installed Rails version 2 on my Mac.

The solution is to roll back, and downgrade Rails to a previous version.

Open up terminal

First check the exact version of Rails you are using

rails -v

Uninstall that version, for example, mine is 2.0.2

sudo gem uninstall -v 2.0.2 rails

Then install the previous version you want. I want the last release of version 1, thats 1.2.6

sudo gem install rails -v 1.2.6

–include-dependencies

Now I check again to be sure the correct version is installed

rails -v

If you need to use pagination, and to follow through with Agile Web Development for Rails you will, use:

# Note: you’ll need to have Subversion installed
ruby script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination
ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate

Set up Ruby on Rails with mySQL on OS X Leopard

Tuesday, January 29th, 2008

I decided to start learning Ruby on Rails, apparently one of the best programming languages/frameworks out there. RoR (Ruby on Rails) comes pre-installed on Leopard, but its not the latest version, and its set up to work with SQLlite, not mySQL, which I want to use.

This tutorial will get you the latest version of Rails and let it work with mySQL. I have another tutorial which covers how to install mySQL.

1 - Ensure you have installed X-code

This won’t work without XCode, so install the correct version first, from the Leopard DVD.

2 - Update Rails on Leopard

To update Rails to the latest version on OS X 10.5 simply open terminal and enter the following command:

sudo gem install rails –include-dependencies

If you want to clean up older versions of rails run this command:

sudo gem cleanup

3 - Install the mySQL Ruby Gem

I only know this works with the MySQL binary installation described here. So be warned!

sudo -s
ARCHFLAGS=”-arch i386″ gem install mysql — –with-mysql-dir=/usr/local/mysql

Pick the latest version of mySQL for Ruby when asked :

Select which gem to install for your platform (universal-darwin9.0)
1. mysql 2.7.3 (mswin32)
2. mysql 2.7.1 (mswin32)
3. mysql 2.7 (ruby)
4. mysql 2.6 (ruby)
5. Skip this gem
6. Cancel installation

I picked 3 - mySql 2.7 as thats the latest.

If you suffer issues try this complete guide.

4 - Test it works

  1. Choose the location you want to put the code for a rails app in - I choose /Sites in my home directory in the following example.
  2. Navigate to the folder with terminal:
    cd Sites
  3. Issue the rails set up command with database parameter:
    rails appname –database=mysql
  4. Create a database called appname_development with your preferred tool such as cocoaMySQL
  5. Ensure there is a user called root without a password, if not, you will need to alter the file at appname/config.database.yml
  6. Navigate to the appname folder in terminal:
    cd Sites/appname
  7. Run:
    rake db:migrate
  8. Then startup
    ruby script/server
  9. And go to locahost:3000 to see the app in action

If you have some problems, it is likely because the default binary mySQL from the website is not fully compatible at first with Ruby. To fix the problems, check out my post on setting up MySQL on a Mac.

This site was created with Wordpress, using my own template.