Showing posts with label Ruby on Rails. Show all posts
Showing posts with label Ruby on Rails. Show all posts

November 15, 2008

mod_rails (Passenger) setup issues with Apache2 on OS X

mod_rails not finding Apache2 APR



When installing mod_rails with a custom Apache2 setup, you may get the following while running sudo passenger-install-apache2-module:


Compiling and installing Apache 2 module...
cd /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3
rake clean apache2
(in /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3)
rake aborted!
Could not find Apache Portable Runtime (APR).
/usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/rakefile:37
(See full trace by running task with --trace)



The main reason for this is that mod_rails cannot find apr-1-config which should have been installed within (as per my system) /usr/local/apache2/bin. In order to solve the problem, you just need to add the Apache2 custom install bin directory to your user path (as per my system):


export PATH=":/usr/local/apache2/bin:$PATH"


That should do it.

mod_rails and "MACOSX_DEPLOYMENT_TARGET environment variable"


The error you are most likely to get would be close to the following:


g++ -flat_namespace -bundle -undefined dynamic_lookup Utils.o Bucket.o Logging.o System.o Configuration.o Hooks.o mod_passenger.o -fPIC -o mod_passenger.so -lstdc++ -lpthread ../boost/src/libboost_thread.a -L/usr/local/apache2/lib -lapr-1
/usr/bin/ld: flag: -undefined dynamic_lookup can't be used with MACOSX_DEPLOYMENT_TARGET environment variable set to: 10.1
collect2: ld returned 1 exit status
rake aborted!
Command failed with status (1): [g++ -flat_namespace -bundle -undefined dyn...]
/usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/rakefile:142
(See full trace by running task with --trace)


In this case, you will have to add the following to your environment variables


export MACOSX_DEPLOYMENT_TARGET=10.4

October 4, 2007

Ruby on Rails with Aquamacs

A friend of mine recently suggested that I look into Aquamacs. According to the website, Aquamacs is an Aqua-native build of the powerful Emacs text editor.

So, I went ahead and downloaded it. First thing I noticed is that it supported stuff for Ruby development right out of the box as the original Emacs. The only problems is, it's got a hard time working with regular indentations, with tabs and so on. I spent a few hours investigating and picking up hints from many places/sources I've put together a LISP script that makes using Aquamacs really nice for Ruby on Rails development.



; stuff for ruby on rails development
(add-to-list 'load-path
"~/Library/Preferences/Aquamacs Emacs/ruby")
(require 'ruby-mode)

; loads ruby mode when a .rb file is opened.
(setq auto-mode-alist
(cons '(".rb$" . ruby-mode) auto-mode-alist))

(setq auto-mode-alist
(cons '(".rhtml$" . html-mode) auto-mode-alist))

; this allows us to have constant indentation as
; we progress in the code from line to line.
(defun create-newline-and-indent()
(local-set-key [return] 'newline-and-indent))

(add-hook 'ruby-mode-hook 'create-newline-and-indent)


; enables ruby electric for easier editing of
; rb and rhtml files
(add-hook 'ruby-mode-hook
(lambda()
(add-hook 'local-write-file-hooks
'(lambda()
(save-excursion
(untabify (point-min) (point-max))
(delete-trailing-whitespace)
)))

; forces ruby-mode to use tabs for indentation with
; an indent level of 4
(setq indent-tabs-mode 1)
(setq ruby-indent-level 4)

; allows the [tab] key to work with width 4
(define-key ruby-mode-map "\t" 'self-insert-command)
(set (make-local-variable 'tab-width) 4)

(define-key ruby-mode-map "C-m" 'newline-and-indent)

; setting up ruby-electric
(require 'ruby-electric)
(ruby-electric-mode t)
))


The script can just be copied and pasted in your "Preferences.el" file which is used instead of the usual ".emacs.el". The Preferences.el file can be found in "~/Library/Preferences/Aquamacs Emacs".

Hope this saves a few hours for some people out there.

Cheers!

July 18, 2007

Ruby on Rails updates

I just finished updating the Ruby on Rails installation on my laptop.

I am now running the following:

Ruby 1.8.6
gem 0.9.4
Rails 1.2.3

It is interesting to see that I am already dealing with a few deprecations here and there.
It obviously is nothing major but I am currently going over my code to make sure that all
deprecations are resolved.

I think that I should have kept up-to-date a bit more regularly. In fact, I went from Ruby 1.8.4 to Ruby 1.8.6. This shows how long it had been since I updated.

Alright, just to remind all RoR developers out there to make sure things are up-to-date.

Cheers!

June 22, 2007

MySQL Data Types and Rails Migrations

If you have ever tried googling for Rails migrations and MySQL data types in order to understand how they map, you may have been disappointed as I had been. There is very little documentation about that on the RoR Wiki. So, it takes a while to understand how they map, what you can do in your create_table functions and what you cannot do. There is a way to get all that information though. And it's right on your computer.

First, it is important to understand that Rails communicates with databases by using Adapters. What in Java are usually known as JDBC Database Connectors. Adapter classes for databases are usually found in classes of the following format:

ActiveRecord::ConnectionAdapters::DatabaseServerNameAdapter. For which DatabaseServerName could be MySQL, PostgreSQL and so on.

Assuming that you're running OS X or any other type of Unix system, you will be able to find the code for your particular adapter by running:

less /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/mysql_adapter.rb

In my case, my database adapter is MySQL. Once the file opens up, look for the function native_database_types. It shows the mapping between Rails names and your particular database. Again, in the case of MySQL the mapping is as follows:


def native_database_types #:nodoc:
{
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 11 },
:float => { :name => "float" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 }
}
end


It is also interesting to read through the full source code of the adapter. There is a bunch of valuable information in there that one can use to optimize the usage of ActiveRecord models as well as Migrations.

-- JPGeek.

January 20, 2007

Rails LightTPD and fastCGI Startup Error

After a fresh installation of the Ruby on Rails development environment on my Powerbook and a migration of my previous applications into that environment, I was unable to start any of them. There are two ways of starting up an RoR application. The first and simplest way is to use the WebRick HTTP server included in your Rails installation. The command for it is as follows:

  • Windows: ruby script/server webrick
  • Unix/Mac/Linux: script/server webrick
After running the command, you should be able to see your server starting and by directing your browser to [http://localhost:3000], access your application's main page.

The second way of starting up an RoR application is by not specifying the webrick argument on the command line. It would look like this:

  • Windows: ruby script/server
  • Unix/Mac/Linux: script/server
What happens in this case is that the script will lookup your LightTPD installation and try to startup the rails application using it. Most of the time, this would work right out of the box. Sometimes however, if you've installed fastCGI with LightTPD, you might run into the following error:


=> Booting lighttpd (use 'script/server webrick' to force WEBrick)
=> Rails application started on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server (see config/lighttpd.conf for options)
/Users/jeanpaul/webworkspace/ToDo/public/../config/boot.rb:18:in `require': No such file to load -- rubygems (LoadError)
from /Users/jeanpaul/webworkspace/ToDo/public/../config/boot.rb:18
from /Users/jeanpaul/webworkspace/ToDo/public/../config/environment.rb:11:in `require'
from /Users/jeanpaul/webworkspace/ToDo/public/../config/environment.rb:11
from /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi:21:in `require'
from /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi:21
2007-01-20 00:51:56: (mod_fastcgi.c.1048) the fastcgi-backend /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi failed to start:
2007-01-20 00:51:56: (mod_fastcgi.c.1052) child exited with status 1 /Users/jeanpaul/webworkspace/ToDo/public/dispatch.fcgi
2007-01-20 00:51:56: (mod_fastcgi.c.1055) if you try do run PHP as FastCGI backend make sure you use the FastCGI enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT (cgi) NOR (cli)
For more information check http://www.lighttpd.net/documentation/fastcgi.html#preparing-php-as-a-fastcgi-program
2007-01-20 00:51:56: (mod_fastcgi.c.1060) If this is PHP on Gentoo add fastcgi to the USE flags
2007-01-20 00:51:56: (mod_fastcgi.c.1356) [ERROR]: spawning fcgi failed.
2007-01-20 00:51:56: (server.c.834) Configuration of plugins failed. Going down.



If you took a chance and directed your browser to [http://localhost:3000], you might see a [500 - Internal Server Error] being displayed.

What all these errors suggest is that your rubygems path is wrong or that you did not install rubygems at all. After spending quite sometime struggling with this, I found out that all that needed to be done was to change the path to ruby in your [dispatch.fcgi] file. In order to get that done, run the following command on your terminal / command prompt:
  • which ruby
In my case, the returned result was [/usr/local/bin/ruby]. It might be slightly different in yours but this does not really matter. Take note of the path to ruby as displayed on your terminal.

The next step is to open the [dispatch.fcgi] file and change the path to ruby at the beginning of the file to what had been displayed on the terminal. The [dispatch.fcgi] file usually sits in the [public] folder of your rails application. When you open the file, you should see something similar to the following line at the beginning of it:
  • #!/usr/bin/ruby
You will need to change that line from whatever was there to the following:
  • #!/your/path/to/ruby/as/known/by/your/computer
  • In my case, that would be #!/usr/local/bin/ruby
Save the file you just edited and restart your server. If you do not get any further warnings or errors displayed on your terminal after starting up the server, you can safely direct your browser to [http://localhost:3000] and you should be able to see your application running.

Voila! You just got Rails to run with LightTPD and mod_fcgi. ;-)