RORGURU.COM
 

How to build a simple Rails application

 

Now we are ready to build a simple Rails application. In the following section we are going to show you how to build a simple Rails application step-by-step

Open a console window and create a folder called /home//Rails_app

command: mkdir Rails_app

now open that Rails_app folder/Rails_app

command: cd Rails_app

Step 1 :

Now you will be in username@username-desktop:~/Rails_app$

Now its time to create a brand new rails application called my_first_rails_app

username@username-desktop:~/Rails_app$ rails my_first_rails_app -d mysql <-d mysql >
It will specify that which database adapter you want to use ( mysql, oracle, sqlite etc )

Output : When you will run this command you will get output something like this :

create

create app/controllers

create app/helpers

create app/models

create app/views/layouts

create config/environments

create config/initializers

create config/locales

create db

create doc

create lib

create lib/tasks

create log

create public/images

create public/javascripts

create public/stylesheets

create script/performance

create script/process

create test/fixtures

create test/functional

create test/integration

create test/performance

create test/unit

create vendor

create vendor/plugins

create tmp/sessions

create tmp/sockets

create tmp/cache

create tmp/pids

create Rakefile

create README

create app/controllers/application.rb

create app/helpers/application_helper.rb

create test/test_helper.rb

create test/performance/browsing_test.rb

create config/database.yml

create config/routes.rb

create config/initializers/inflections.rb

create config/initializers/mime_types.rb

create config/initializers/new_rails_defaults.rb

create config/locales/en.yml

create config/boot.rb

create config/environment.rb

create config/environments/production.rb

create config/environments/development.rb

create config/environments/test.rb

create script/about

create script/console

create script/dbconsole

create script/destroy

create script/generate

create script/performance/benchmarker

create script/performance/profiler

create script/performance/request

create script/process/reaper

create script/process/spawner

create script/process/inspector

create script/runner

create script/server

create script/plugin

create public/dispatch.rb

create public/dispatch.cgi

create public/dispatch.fcgi

create public/404.html

create public/422.html

create public/500.html

create public/index.html

create public/favicon.ico

create public/robots.txt

create public/images/rails.png

create public/javascripts/prototype.js

create public/javascripts/effects.js

create public/javascripts/dragdrop.js

create public/javascripts/controls.js

create public/javascripts/application.js

create doc/README_FOR_APP

create log/server.log

create log/production.log

create log/development.log

create log/test.log

Step 2 :

Now go to your applications root directory using the following command :

command: cd my_first_rails_app

username@username-desktop:~/Rails_app/my_first_rails_app$

CREATE DATABASE:

Go to your /my_first_rails_app/config and open database.yml file

If you have installed your mysql with password then please provide your password in

development:

adapter: mysql

encoding: utf8

database: my_first_rails_app_development

pool: 5

username: root

password:

< your password >

socket: /var/run/mysqld/mysqld.sock

now run the command

command: rake db:create

username@username-desktop:~/Rails_app/my_first_rails_app$ rake db:create

it will create your database.

Step 3 :

Now run the command :

command: ruby script/server

Output : You will get output like this

=> Booting Mongrel (use 'script/server webrick' to force WEBrick)

=> Rails 2.2.2 application starting on http://0.0.0.0:3000

=> Call with -d to detach

=> Ctrl-C to shutdown server

** Starting Mongrel listening at 0.0.0.0:3000

** Starting Rails with development environment...

** Rails loaded .

** Loading any Rails specific GemPlugins

** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).

** Rails signals registered. HUP => reload (without restart). It might not work well.

** Mongrel 1.1.5 available at 0.0.0.0:3000

** Use CTRL-C to stop.

Step 4 :

Now open your web browser and type the URL as "http://localhost:3000/"

You will view a page with rails logo and welcome message.

That means your application is running properly.

Step 5 :

Now open another console and go to again your application root.

Then type

command: ruby script/generate scaffold user name:string email:string age:integer

Output : You will get output like this

exists app/models/

exists app/controllers/

exists app/helpers/

create app/views/users

exists app/views/layouts/

exists test/functional/

exists test/unit/

exists public/stylesheets/

create app/views/users/index.html.erb

create app/views/users/show.html.erb

create app/views/users/new.html.erb

create app/views/users/edit.html.erb

create app/views/layouts/users.html.erb

create public/stylesheets/scaffold.css

create app/controllers/users_controller.rb

create test/functional/users_controller_test.rb

create app/helpers/users_helper.rb

route map.resources :users

exists app/models/

dependency model

exists app/models/

exists test/unit/

exists test/fixtures/

create app/models/user.rb

create test/unit/user_test.rb

create test/fixtures/users.yml

create db/migrate

create db/migrate/20090205074924_create_users.rb

step 6.

Then type

command: rake db:migrate

it will create your table using the file db/migrate/XXXXXXXXXXX_create_users.rb and after that whenever you want to create a table first create a migration file using command : ruby script/generate migration < name example: create_users>

Step 7:

Now your application is ready for a complete CRUD ( create, read, update, delete ) over users table. Go to your console and stop your web server using < ctrl+c >. Then again run your web server using

command: ruby script/server

Just go to your web browser and paste http://localhost:3000/users and enjoy full CRUD functionality.