What is Ruby?
Ruby is a pure object-oriented programming language with a super-clean syntax that makes programming elegant and fun. Ruby successfully combines Smalltalk’s conceptual elegance, Python’s ease of use and learning, and Perl’s pragmatism. Ruby originated in Japan in the early 1990s. It has become popular worldwide in the past few years as more English-language books and documentation have become available (and its popularity has really taken off since the introduction of Rails!).
What is Rails?
Rails is an open source Ruby framework for developing web-based, database-driven applications. What’s special about that? There are dozens of frameworks out there, and most of them have been around much longer than Rails. Why should you care about yet another framework?
What would you think if I told you that you can develop a web application at least ten times faster with Rails than you can with a typical Java framework? You can–without making any sacrifices in the quality of your application! How is this possible?
Part of the answer lies in the Ruby programming language. Rails takes full advantage of Ruby. The rest of the answer is in two of Rails’ guiding principles: less software and convention over configuration.
Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden.
Convention over configuration means an end to verbose XML configuration files–there aren’t any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know!
Okay so basicly Ruby is the programming language it’s like the new php or something and the on rails part appears to be a developer software so like Visual Studio but for this ruby language…
General Features
Rails has some general and some specific characteristics.
Web servers
Rails can run on just about any web server that implements CGI. However, the performance of CGI is notoriously bad, so the preferred deployment of a Rails application is to use FastCGI. There have been extensive tests of Rails application deployments with both Apache and LightTPD. There is also a newcomer, SCGI, which rivals the performance of FastCGI without the complicated setup.
During development, it is usually easiest just to use the WEBrick web server that comes built-in to Ruby.
Databases
Rails currently contains support for the following databases:
- MySQL
- PostgreSQL
- SQLite
- SQL Server
- DB2
- Oracle
It takes approximately 100 lines of Ruby code to implement a database adapter, so adding to this list is not particularly onerous.
Debugging
When something goes wrong inside your Rails web app, you normally get a pretty detailed error display in your browser (when running in development mode). Often this is enough to diagnose the problem. When it’s not, you have other options:
-
Insert debugging output in your controller. For example:
render_text "Got here"
or
render_text "user object = " + user_obj
- Check the Rails log files. (Use
tail
on *nix systems.) Look for the files development.log, production.log, and fastcgi.crash.log. Remember that your web server has log files, too. - Use breakpoints.
- Use a commercial IDE (like ArachnoRuby) with a built-in debugger.
Custom (pretty) URLs
The default Rails mapping of URLs to controller actions is very simple and easy to understand. Rails tries very hard to present the user with pretty URLs. Rails URLs are simple and straightforward, not long and cryptic.
Even so, you can still customize your URLs by using the Rails routing facility. Rails’ URL routing is flexible enough to allow you to create virtually any URL mapping scheme.
The Rails routing facility is pure Ruby code that even allows you to use regular expressions. Because Rails does not use the web server’s URL mapping (like mod_rewrite
in Apache), your custom URL mapping will work the same on every web server.
Unit testing
Rails actively facilitates unit testing:
- Generating new controllers, models, and scaffolding also creates corresponding unit test skeletons.
- The strict MVC architecture tends to naturally result in testable actions and components.
- Rails includes a Rake (Ruby Make) script that will automatically run all your unit tests.
Let’s Start the Installation
Intall the dependencies:
- Install the apache
yum install httpd-devel httpd apr apr-devel apr-util-devel emacs-common mysql-devel
2. Install the mysql database
yum install mysql-server
Before i install the fastcgi i want to explain it first,
What web server can I use with Rails?
Apache and Lighttpd are the most popular web servers for running Rails. But any web server that supports mod_ruby (Apache), CGI, SCGI, or FastCGI? can be used.
Apache is the reference web server and will be the one upon which all examples are based.
You may also want to consider using Lighttpd which is comparable in speed to Apache 2.0.54 using MPM Worker on Debian 3.1—this is considerably faster than Apache 1.x or 2.x MPM Prefork. WARNING: Lighttpd-1.4.10 is very buggy—use Lighttpd-1.4.11 or newer version.
A newer option is to use Zed Shaw’s Mongrel . Mongrel is a ruby based webserver that uses C extensions to dramatically improve performance compared to Webrick. Mongrel can be run independently or in a cluster behind a Lighttpd or Apache proxy.
Which is preferred, mod_ruby, SCGI or FastCGI??
We’ll divide the answer in 2 parts (FastCGI and non-FastCGI).
For FastCGI:
- with Apache 1.x, use mod_fastcgi.
- with Apache 2.x, use mod_fcgid.
- with Lighttpd, use the bundled mod_fastcgi
NOTE: mod_fcgid is very active and mod_fastcgi hasn’t been updated in years.
For non-FastCGI
- mod_ruby was recently updated and is a good solution when you don’t share your Apache server with others
- SCGI is a simpler technology than FastCGI which is available for most web servers.
I will install RubyOnRails using FastCGI
3. Install FastCGI
a. Download the fcgi package
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
b. Unrar the package
tar -xzf fcgi-2.4.0.tar.gz
c. Go to fcgi folder and install it
cd fcgi-2.4.0
./configure
make
make install
4 Install mod_fcgid
a. Download the mod_fcgid package
wget http://fastcgi.coremail.cn/mod_fcgid.1.09.tar.gz
b. Unrar the package
tar -xzf mod_fcgid.1.09.tar.gz
c. Go to mod_fcgi folder and install it
cd mod_fcgid.1.09
vi Makefile
Change top_dir to: top_dir = /usr/lib/httpd Uncomment #INCLUDES and change to INCLUDES=-I /usr/include/httpd -I /usr/include/apr-0make
make install
4. Install Ruby
a. Incase the ruby was not available by default you can set it on your repository
cd /etc/yum.repos.d/
wget http://dev.centos.org/centos/4/CentOS-Testing.repo
b. Install the ruby
yum --enablerepo=c4-testing install ruby ruby-docs ri ruby-libs ruby-mode ruby-tcltk irb rdoc ruby-devel
Note: you may need to install ruby-irb instead of irb from the testing repo if version numbers do not match up
yum --enablerepo=c4-testing install ruby ruby-docs ri ruby-libs ruby-mode ruby-tcltk ruby-irb rdoc ruby-devel
5. Install Ruby Gems
RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them. RubyGems is now part of the standard library from Ruby version 1.9.
a. Download the ruby gems package
wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
b. Unrar the package
tar -xzf rubygems-0.9.0.tgz
c. Go to Ruby gems folder and setup it and update it
cd rubygems-0.9.0
ruby setup.rb
gem update
Install the Rails through gems
gem install rails --include-dependencies
When i install / update the Rails through gem on my Openvz vps (CentOS release 4.6 (Final)) i got this error :
[root@adityo2 rubygems]#gem update Updating installed gems... Bulk updating Gem source index for: http://gems.rubyforge.org /usr/lib/ruby/1.8/yaml.rb:133:in `load': failed to allocate memory (NoMemoryError)
After some googling it turns out that Gem need memory more than 256mb, then i try to changes my VPS memory to 1 Gb
vzctl set 20 --save --vmguarpages 262144 vzctl set 20 --save --oomguarpages 262144 vzctl set 20 --save --privvmpages 262144
then i restart the VPS
vzctl stop 20 vzctl start 20
;20 is my vps id
then i try the rails gem installation again
gem install rails --include-dependencies
then install the fcgi through gem
gem install fcgi
then install mysql
gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
Building native extensions. This could take a while...
Successfully installed mysql-2.7
1 gem installedvi /etc/ld.so.conf
Add line: /usr/local/lib/sbin /ldconfig
Configure Apache
vi /etc/httpd/conf.d/fcgid.conf
Insert the following:
LoadModule fcgid_module /usr/lib/httpd/modules/mod_fcgid.so <IfModule mod_fcgid.c> SocketPath /tmp/fcgid_sock/ AddHandler fcgid-script .fcgi </IfModule>
2. Restart the httpd
service httpd restart
It should install the RubyOnRails
How to Test Ruby On Rails
I will create a simple Test Application on my /home/praetorian-id.org/public_html/rails, i will start it by creating a simple project folder first on /home/praetorian-id.org/public_html/rails using this command
rails MyProject
By default
Rails 2.0.2 uses sqlite3 by default. Either you:
1. Install sqlite and install the sqlite-ruby gem.
2. Specify rails -d mysql <project name> when creating your projects.
3. Manually change it in config/database.yml
in this example i will create MyProject database
rails -d mysql MyProject
To configure Rails not to bother with a database, open Rails’ main config file located in config/environment.rb in your text editor of choice and find the (commented out) line that reads:
config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
Remove the comment and modify the line so that it excludes only ActiveRecord.
config.frameworks -= [ :active_record ]
then enter to MyProject folder
cd MyProject
then i will run the RubyOnRails
./script/server
=> Booting WEBrick... => Rails 2.1.0 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options [2008-08-28 08:33:04] INFO WEBrick 1.3.1 [2008-08-28 08:33:04] INFO ruby 1.8.5 (2006-08-25) [i386-linux] [2008-08-28 08:33:04] INFO WEBrick::HTTPServer#start: pid=23942 port=3000
61.247.2.31 - - [28/Aug/2008:08:33:17 MSD] "GET /? HTTP/1.1" 304 0
- -> /?
61.247.2.31 - - [28/Aug/2008:08:33:17 MSD] "GET /javascripts/prototype.js HTTP/1.1" 304 0
http://208.99.198.190:3000/? -> /javascripts/prototype.js
61.247.2.31 - - [28/Aug/2008:08:33:18 MSD] "GET /javascripts/effects.js HTTP/1.1" 304 0
http://208.99.198.190:3000/? -> /javascripts/effects.js
61.247.2.31 - - [28/Aug/2008:08:33:18 MSD] "GET /images/rails.png HTTP/1.1" 304 0
http://208.99.198.190:3000/? -> /images/rails.png
61.247.2.31 - - [28/Aug/2008:08:33:21 MSD] "GET /rails/info/properties HTTP/1.1" 500 10353
http://208.99.198.190:3000/? -> /rails/info/properties
see the Ruby On Rails pages on http://localhost:3000
Ruby On Rails Common Error :
1.
error detail :
This error occurred while loading the following files:
sqlite3
Error was cause Ruby On Rails are using sqlite3 ,
By default
Rails 2.0.2 uses sqlite3 ,Solution =
1. Install sqlite and install the sqlite-ruby gem.
2. Specify rails -d mysql <project name> when creating your projects.
3. Manually change it in config/database.yml
2.
error detail :
Access denied for user 'root'@'localhost' (using password: NO) Error was cause by the Ruby on rail database config password did not matched solution : go to your project folder -> /config -> /database.yml # vi /home/lineabsolute.com/public_html/rails/myrailsapp/config/database.yml
test:
adapter: mysql
encoding: utf8
database: myapp_test
pool: 5
username: root
password: xxxxx -> enter the correct password
socket: /var/lib/mysql/mysql.sock
production:
adapter: mysql
encoding: utf8
database: myapp_production
pool: 5
username: root
password: xxxxx -> enter the correct password
socket: /var/lib/mysql/mysql.sock
3.
error detail : Unknown database 'myapp_development'
error was cause by the myapp_development db was not created yet
solution :
mysql -u root -p
mysql> create database myapp_development;
Query OK, 1 row affected (0.01 sec)
mysql> exit