Creates basic skeleton of the application with rails command
rails -d mysql library
cd library
Run the script/server
script/server
Creating Active Record Files (Model)
ruby script/generate model Book
ruby script/generate model Subject
Apart from creating many other files and directories, this will create files named
book.rb and subject.rb containing a skeleton definition in app/models directory.
Now you need to tell Rails what relationships you want to establish within the library
data system. Modify book.rb and subject.rb according to association required.
Validations can be done within these files.
Create Migration
ruby script/generate migration table_name
This will create the file db/migrate/001_table_name.rb. A migration file contains basic
Ruby syntax that describes the data structure of a database table.
ruby script/generate model Book title:string price:integer subject_id:integer
description:text
It will define the probable structure that we require in our database table, in file created
in db/migrate which was created at the time of creating model. Second way, We can do this
directly by adding structure in those model files.
ruby script/generate migration books
This will generate migration file for that table(books).
script/generate model Subject name:string
ruby script/generate migration subjects
This will be the same as above but for different model.
Now you have created all the required migration files, its time to execute them against
the database.
Run the Migration
rake db:migrate
This will create following three tables in library_development database:
1. The books table if it doesn't exist.
2. The subjects table if it doesn't exist.
3. The schema_migrations table if it doesn't exist which tracks the current version of the
database - each new migration will be a new version, and any new migrations will be
run until your database is at the current version.
Selection of the Rail Environment you want to work with..
set RAILS_ENV=development
rake db:migrate
If you would like to specify what rails environment to use for the migration, use the
RAILS_ENV shell variable.
Now we have our Database and required Tables available. In the two subsequent
chapters we will explore two important components called Controller (ActionController) and
View (ActionView).
1. Creating Controllers ( Action Controller )
2. Creating Views ( Action View )
Create Controllers (Action controller)
ruby script/generate controller Book
We will create just one controller here. This command accomplishes several tasks, of
which the following are relevant here
It creates a file called app/controllers/book_controller.rb
It creates a file called app/controllers/application.rb
Controller classes inherit from ApplicationController, which is the other file in the
controllers folder: application.rb.
You can create methods in book_controller.rb
Create View (Action View)
Rails lets you know that you need to create the view file for the new method. Each
method you define in the controller needs to have a corresponding .html.erb file, with the same
name as the method, to display the data that the method is collecting.