, , , , , ,

Hello Guys this is My First, Post On Rails, Here I will Describe You how to create Scaffold on Rails. For those who are Unfamiliar with the Concept of Scaffold, Let me tell You A short Discussion about Scaffolding, Scaffolding is a basic Skeleton of your Application

For Example, if You want your HTML page will consist of Fist Name , Last Name, Email, Password, Address Etc. Scaffolding in rails will do that for you in few seconds, You don’t need to write Code for that.

Now, Let me Describe you, how scaffolding works..

Fist of all Check Your Rails Version, if You are Using Rails Version Greater than 1.2.5 you can’t create scaffolding in that, for that You Must have rails version 1.2.5

You can also take run two versions of rails at the same time.

To install rails Version 1.2.5 on your machine, type in the Konsole/Command Window :

for linux users : sudo gem install rails v=1.2.5
for windows users : gem install rails v=1.2.5

Now check your rails Version by Typing :

for both Linux/Windows User :rails -v

if your Konsole/Command Window shows this :
rails(2.0.2, 1.2.5)
means you have both rails version installed in your machine

in case if it shows this :
rails 1.2.5
you can install another version of rails by using following command :

for linux users : sudo gem install rails v=2.0.2
for windows users : gem install rails v=2.0.2

For those who are Installing rails first time on their machine, I recommand you to use both version of rails, to save extra work.

Now we have done with rails, the second step is to make an Application :
if you wish you can create you own folder where you can keep your all rails application

for me, I am Keeping all my rails applications on a Rails_Application folder

Now go to Konsole/command window..

Go to the directory where u want to keep your all rails application

(From here both Linux/Windows user follow these steps 🙂
(this is my Directory)puneet@puneet-laptop:/home/windows1/Rails_Application$ rails sample

This Will create the following directories in sample folder
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
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/mocks/development
create test/mocks/test
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 config/database.yml
create config/routes.rb
create public/.htaccess
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
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/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

Now go into your sample project by typing :
puneet@puneet-laptop:/home/windows1/Rails_Application$cd sample


Now go into your database, here i m using MySql Database, and Create a new schema, named sample_development

You can also create a schema through konsole/command window by typing :
(for both Linux/Windows user) :
puneet@puneet-laptop:/home/windows1/Rails_Application/sample$mysqladmin -u root create sample_development

this will create a new schema on your mysql database

Now the Important thing :

for those users who are using both rails version i.e(1.2.5, 2.0.2),
go to: sample/config/database.yml… and open that file

You will find the code like this :

# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000

# Warning: The database defined as ‘test’ will be erased and
# re-generated from your development database when you run ‘rake’.
# Do not set this db to the same as development or production.
adapter: sqlite3
database: db/test.sqlite3
timeout: 5000

adapter: sqlite3
database: db/production.sqlite3
timeout: 5000

Now follow the steps as I am doing, because this is the configuration code for mysql that support rails version 2.0.2 only, and you will not be able to create scaffold.

Follow this (for both Linux/Windows User) :

adapter: mysql
database: sample_development
username: root(your mysql user name)
password: (Your Mysql Password)
socket: /var/run/mysqld/mysqld.sock

adapter: mysql
database: sample_test
username: root(your mysql user name)
password: (Your Mysql Password)
socket: /var/run/mysqld/mysqld.sock

adapter: mysql
database: sample_production
username: root(your mysql user name)
password: (Your Mysql Password)
socket: /var/run/mysqld/mysqld.sock

Now go into you sample/config/environment.rb and open it :

You Will See the following line of code written there :

RAILS_GEM_VERSION = ‘2.0.2’ unless defined? RAILS_GEM_VERSION

Replace this line with this:

RAILS_GEM_VERSION = ‘1.2.5’ unless defined? RAILS_GEM_VERSION

and at the bottom of the environemt.rb file which you have replaced the following code is also written:

config.action_controller.session = {
:session_key => ‘_myapp_session’,
:secret => ‘f088b01f77b99701afe5fb198ed40320c31df263a4d5885f9747f426a8b1d61170ffc3d69cfc617d3c637449b1bdf9ebcf0284e396ec22c84b99f2288f4b18c4′

Comment All this, or you can also remove those lines, but I recommend you to comment this. That’s all in environment.rb

Now go to app/contollers/application.rb and open it, you will find the code like this:

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you’re not using the cookie session store
protect_from_forgery # :secret => ’91fa0a704e62068793680a6a0b37d7cf’

Comment these two lines
#helper :all # include all helpers, all the time
#protect_from_forgery # :secret => ’91fa0a704e62068793680a6a0b37d7cf’

To create a Scaffold, You Need to create a Model, Only After that you can create scaffold, there are some more way, but I am describing you the easiest way to do this.

Go into you command/konsole window, then go to your application directory:

and then type:

ruby script/generate model User

this will create following files:
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/001_create_users.rb

Now Open sample/db/001_create_users.rb, and add the following code:

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :firstname, :string
t.column :lastname, :string
t.column :email, :string
t.column :password, :string
t.column :dob, :date
t.column :profile, :text

def self.down
drop_table :users

Note: before migrating your tables into your database, be sure that your database must contain a schema called sample_development.

If this schema doesn’t exist you can create manually on ur Mysql Server, or you can create manually by running this command:

mysqladmin -u root create sample_development

Now go into your rails application directory, through konsole/command window and type this:

puneet@puneet-laptop:/home/windows1/Rails_Application/sample$ rake db:migrate

this command will migrate your following columns into your database, the o/p will look like this:
(in /home/windows1/Rails_Application/sample)
== CreateUsers: migrating =====================================================
— create_table(:users)
-> 0.0320s
== CreateUsers: migrated (0.0325s) ============================================

Now the Magic Comes, Type
puneet@puneet-laptop:/home/windows1/Rails_Application/sample$ ruby script/generate scaffold User

You Will See the following directories and files will create:

exists app/controllers/
exists app/helpers/
create app/views/users
exists app/views/layouts/
exists test/functional/
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
identical app/models/user.rb
identical test/unit/user_test.rb
identical test/fixtures/users.yml
create app/views/users/_form.rhtml
create app/views/users/list.rhtml
create app/views/users/show.rhtml
create app/views/users/new.rhtml
create app/views/users/edit.rhtml
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
create app/views/layouts/users.rhtml
create public/stylesheets/scaffold.css

That’s it, You have done it, Open your favourite browser, but don’t forget to start your mongrel/WeBrick Server by typing

sudo script/server

in the url type: http://localhost:3000/users/new

and see the rails magick.. isn’t it great!! the fields will also saved on each crate button.

Note: what is the problem in creating scaffold with rails v=2.0.2,

Solution: Each time when you type ruby script/generate scaffold User, All the files will be created, but the controller will not create, So Nothing will Work without a Controller. That’s why I write this post