Following in the footsteps of CakePHP there is Ruby on Rails which is also a MVC framework. It is based on the Model, View, Controller (MVC) philosophy:
To install Ruby on Rails get all the usual LAMP modules, i.e.:
apt-get install apache2 php5 mysql-client mysql-server php-mysql
Restart Apache2 to be safe with the command:
/etc/init.d/apache2 reload
There is a really detailed explanation of how to do this on Ubuntu
here. Important, because you are a beginner stop after step 6,
where you type "gem install rails". For testing keep the
Webrick server, but later you may want to install the mongrel server by typing:
gem install mongrel
To launch it, go to where you have installed your application, i.e.
cd /var/www/myapp/blog
mongrel_rails start -p 800 -e production -d
Note, this way you can use port 800 for example, instead of port 3000 which the Webrick likes to use.
Note, I did not choose port 80 because that is the normal apache2 localhost port.
It is real important to verify that you have everything running doing the Webrick test, that
is go to a terminal prompt and type the following:
cd /var/www/myapp/blog
Note, I created the blog Ruby test application using the Linux tutorial mentioned at the end of
this page. In the directory type:
script/server to launch the Webrick server. Then when you go to:
http://localhost:3000 in your web browser and click on the about your applications environment
you shoud see all of this:
Ruby version 1.8.7 (i486-linux)
RubyGems version 1.3.5
Rack version 1.0
Rails version 2.3.4
Active Record version 2.3.4
Active Resource version 2.3.4
Action Mailer version 2.3.4
Active Support version 2.3.4
Application root /var/www/myapp/blog
Environment development
Database adapter mysql
Database schema version 0
If you do not see all this, check the load order for any web servers that you have, i.e.:
/etc/init.d/apache start
script/server from your /var/www/myapp/blog directory
I found that if I reversed the load order of the above, I would get an error. It is darn finicky, if
you ask me. This is the hardest part of the entire Ruby project. You can waste days, thinking something
is wrong when it is just the server order load.
After you have successfully done that you can then go to
this
tutorial. It is an O'Reilly tutorial, so you know you can trust it. Note, it
assumes you are on Microsoft Windows. It also was written several years ago for a much older version
of ruby/rails. Here are some of the changes you need to be aware of:
Instead of going through the mysql PHPMyAdmin GUI, you can just type at a mysql prompt:
use cookbook_development;
You no longer use the edit.rhtml or any file with a .rhtml extension. The list method is autogenerated
for you with the file index.html.erb (under /home/var/www/myapp/cookbook/app/views/recipes)
mentioned in more detail in the to update here link further
on.
To generate your table use this
create_recipes.sql file by typing:
source create_recipes.sql
at the mysql prompt. Note, I have called the table
recipes_dup so I do not overwrite one you may have already created. You can change the
name of the table generated in the sql script file.
Scaffold is no longer a valid method in Ruby so do this instead of modifying the
recipe.rb file. Go to the /var/www/myapp/cookbook directory and type:
script/generate scaffold Recipe id:int title:string instructions:text
Please pay attention to the case of the letters I typed so you match things as
they are defined in the recipes table. Now, everything should come up fine, when
you go to http://localhost:3000/recipes.
Also note, the forms are not automatically changed after you change the table by adding new columns. You
need to rerun the scaffold generator as in:
script/generate scaffold Recipe id:int title:string description:string date:date instructions:text
To create a nice interface, use this
index.html.erb file which is located in
/var/www/myapp/cookbook/app/views/recipes directory, instead of the list.rhtml file that O'Reilly
suggests.
All of the previous gets you to the point where you have two seperate models one for recipe
and one for category . Now comes the the really tricky part . A relationship will be formed between
the two modes. Meaning that a recipe can have a category ( desert, main meal, beverage etc.). Similarly a category
can have many recipes. This is demonstrated in the model files recipe.rb and category.rb.
/var/www/myapp/cookbook/app/models files
recipe.rb **
class Recipe < ActiveRecord::Base
belongs_to :category
end
category.rb **
class Category < ActiveRecord::Base
has_many :recipes
end
This information needs to also be in the routes.rb file:
/var/www/myapp/cookbook/config/routes.rb
routes.rb **
ActionController::Routing::Routes.draw do |map|
map.resources :recipes
map.resources :categories, :has_many => :recipes
I have assembled all the files for you that you will need
to update here.
The majority of these files will be generated by the two
scaffolding commands:
script/generate scaffold Recipe id:int title:string instructions:text category_id:int
script/generate scaffold Category id:int name:string
Note, this is more or less identical to what you did before. Your models and controllers will be generated by:
ruby script\generate model Recipe
ruby script\generate controller Recipe
ruby script\generate model Category
ruby script\generate controller Category
After doing these you need to do some edits to all the files generated so that they match what I have given
you in the to update here. You could just copy them all in and they will work, but take the time one by one
to compare what was generated to the changes I made.
Here are some of the most important things that I did:
You may prefer for Linux this tutorial. It is equally as good as the O'Reilly one and a little more up to date.
Note, the very first Ruby on Rails install tutorial, mentioned in the very beginning of this web page,
suggested a different application root
directory. I found
/var/www/myapp/YourRubyApplication
worked best.
Note, the posts_controller.rb file used in the Linux tutorial example, I have posted
here . This is slightly different from the example solution. Later in the tutorial for Linux,
he introduces Tags and Comments to enhance the application. I have worked through the code several times
and found that it is not complete. You still have to fill in a lot of the views, etc. I would suggest stopping when you
get to that point.
Enjoy!