Emulated ruby tags (ERB) tags

Some handy information about working with those less-than-percent sign things that appear in Emulated Ruby, or .rhtml files.

example what it does
<% ... %> evaluate/execute ruby code
<%= ... %> evaluate & return to buffer (result will be included in output)
<%- ... %> strip whitespace before tag
<% ... -%> strip whitespace after tag

Rails Script shortcuts

Some great shortcuts for script generation. I always try to start by building scaffold for any new entity, then remove the scaffold as needed, leaving only the pieces of the scaffold which I need.

to generate a new model:
script/generate scaffold Person age:integer first:string last:string some_list_id:integer

* This will generate models, controllers, migrations & tests. ALWAYS USE THE SINGULAR when using script/generate.

* Be sure to spell the complete word integer, if you mistakenly shorten it to abbreviated form “int” the generate command will look like it has worked, but migration won’t execute.

If you make a mistake try
script/destroy (model name)

to add a field to an existing model
script/generate migration add_address_to_person address:string

to add a foreign key to an existing model
script/generate migration add_state_id_to_person state_id:integer


Passenger with Rails

Passenger is a great thing because it makes Rails apps able to be deployed on Apache.

The installation is pretty painless, but there are a few gotchas you may want to know about.

Start here: http://www.modrails.com/install.html

sudo gem install passenger

The installation will prompt you to add this to your httpd.conf file: (note the version number of passenger might be different)

LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-2.1.3/ext/apache2/mod_passenger.so
PassengerRoot /Library/Ruby/Gems/1.8/gems/passenger-2.1.3
PassengerRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby

If you are NOT using the Passenger preference pane, follow these instructions to add a virtual host to your Apache configuration file, and set its DocumentRoot to /somewhere/public. If you ARE using the passenger preference pane, you don’t need to do this.

RailsEnv development
ServerName mysite.local
DocumentRoot /Users/jason/DEVELOPMENT/mysite/public

Options FollowSymLinks
Order allow,deny
Allow from all

When first installing passenger (sudo gem install passenger), I get this error:
Building native extentions. This could take a while…
ERROR: Error installing passenger:
ERROR: Failed to build gem native extensions

FIX: Type ‘make’ at the command prompt. If you get the error “make: command not found,” then you don’t have ‘make’ (gcc compiler) installed, so your system can’t build anything. Install XCode Tools to solve this.

SYMPTOM: Site loads but it does not go to your rails app correctly.

FIX: Check to make sure apache config file, or the file you included in apache config, is correct. Try putting your site at a different port like this:

Listen 8000

Remember to restart apache each time you make a change with:
sudo apachectl restart

SYMTPOM: Browser says “500 Internal Server Error”

FIX: Check the Apache error log (Applications > Utilities > Console, click “Show Log List” if the list of logs does not appear, go to /var/log/apache2/error_log )

SYMPTOM: *** Passenger ERROR:Could not spawn the sever the server… Permission denied

FIX: If you are running a custom ruby executable (see custom ruby executable with passenger), you may not have set your custo ruby executable file to have execute permissions.

SYMPTOM: Routing error … no route for /YourApp

FIX: If you see this in your browser (where /YourApp is the name of your app), it is probably because you did not have this in your Rails config/environment.rb file:

Rails::Initializer.run do |config|

config.action_controller.relative_url_root = “/YourApp”

SYMPTOM: Ruby on rails version ‘2.3.1’ not found
(or some other version)

FIX: Upgrade rails (or install the needed Rails version, more than one can run at once):
sudo gem install rails –version 2.3.1


SYMPTOM: Apache does not respond in the browser.

FIX: Make sure that apache is started ok, try this to see if there are any errors in your httpd.conf file:
sudo apachectl configtest