1. Creation of new App.
/home/user# rails appname
#the app uses SQLite as the default db management system
# a frame work folder is created – name ‘appname’
/home/user# rails -d mysql appname
# running this command instead of the previous one start the app
# with mysql.
Now let us validate the app frame and try to have a knowledge of the directory structure , as in the course
of studies along with programming we are going to understand what all the directories are for.
Also i recomment to have a brief idea about the Model-View-Controller ( MVC )
2. Setting Up Database
mysql> create database appname_development;
> use appname_development;
/* a database is created with name appname_develpoment
‘use’ let the mysql know to use the database created */
> grant all privileges on appname_development.*
-> to ‘root’@’localhost’ identified by ‘password’;
/* here we grant access to db, from root also via localhost with password of root given for authorisation
we can also create new user and assign access right to it…(explained later ) */
> FLUSH PRIVILEGES;
/* usually a change in access hasto be taken in effect from the time of rebooting the dbms…
but to avoid that we do FLUSH PRIVILEGES; */
same is to be repeated with appname_production and appname_test
in ROR while the phase of app ( eg; a Content Management System – CMS ) construction
th db used is appname_development
while testing appname_test
and at the time of production appname_production is used.
configure the file config/database.yml with the password manually for subsequent request processing
about ‘models’ they form the backborn of all the tables created via ROR inteface to Mysql (or any SQL that we are using )
try to remember what we have studied under advanced DBMS about Models and Triggers
files in db/migrate form the class declarations for migrating the table structures on to Mysql database
or creating objects with all the attribute values and datatype specifications
while file in app/models/ acts as the trigger files which are checked for the correctness of data on every aspect
on creation/updation and deletion of a row of a table.
/home/user# ruby script/generate model Apptable
/home/user# ruby script/generate migration Apptable
Here let Apptable be the table we want to migrate on to db appname_delopment
Try not to use numerical values and avoid using ‘_’ for table names
the table name should be singular and should be starting with capital letter
/home/user# export RAILS_ENV=development
/home/user# rake db:migrate
these commands migrate all those classes we have created in /db/migrate
on to table appname_development
repeat the commands with production and test.
4. creating Controllers and Views
/home/user# ruby script/generate controller Index
this creates a controller index_controller.rb in the app/controller folder
and a folder app/view/index were we write our html code with ROR implimentation as *.rhtml file
this Model View Controller archeitecture hides the internal database matters from the user ( not from the developer offcoarse )
write all db read/write operation codes on to the actions in controller
while there corresponding implimentation ( user interface ) codes to their corresponding view file
5. starting Server
finally it comes to starting the server , to view the output of your application
start the server using the command
/home/user/appname# ruby script/server
run the script always from the root of your application ( here it is appname/ )
ruby automatically starts Mongrel or Webrick server packed to ruby on rails
obtained by gem install rails
even though it supports Apache.
now navigate to http://localhost:3000/ to start using your app
navigate to http://localhost:3000/index/start
to run the start action defined in index_controller.rb and having corresponding view file at app/view/index as start.rhtml.
Enjoy programming with Rails.