Introduction To Database, Migration, Controller and Routes in Rails
Introduction To Database, Migration, Controller and Routes in Rails
Introduction to
Database, Migration,
Controller and Routes in
Rails
Aim
To train the students in setting up the MySQL and
PostgreSQL databases and exercise with the active records,
migrations, controllers and routes in Rails
Instructional Objectives
Objectives of this chapter are:
• Illustrate the database setup of MySQL and PostgreSQL in Rails
development: production:
adapter: mysql adapter: mysql
database:library_development database:
username: root library_production
password: [password] username: root
host: localhost test: password: [password]
adapter: mysql host: localhost
database: library_test
username: root
password: [password]
host: localhost
Database Setup for PostgreSQL
To create a new users and password for the new user follow the given commands .
tp> sudo -u postgres createuser rubyuser –s
tp> sudo -u postgres psql
postgres=# \password rubyuser
1) Which command is used for showing current date and time in Mysql
command line tool?
a) select now b) select now( )
c) SELECT NOW( ); d) SELECT NOW( )
2) State whether the given statement is true or false
Sqlite3 is a default database for Ruby on Rails.
a) True b) False
Describe the working
functionality of Rails active
records, migrations, controllers
and routes in Rails
Active Records
To create the Active Record files for the entities of library application, input the following
command from the top level of the application directory.
library\> rails generate model Book
library\> rails generate model Subject
Open and see the book.rb and subject.rb file in any text editor, following template is visible.
book.rb:
class Book < ActiveRecord
end
subject.rb:
class Subject < ActiveRecord
end
Active Records
Creating Associations between Models- To edit or modify the book.rb and subject.rb as follows:
class Book < ActiveRecord class Book < ActiveRecord
belongs_to :subject belongs_to :subject
end end
To run Migration, go to a command prompt and go to the library directory in which the application
is located, and then type the command as follows:
library> rake db:migrate
This will create a "schema_info" table, which tracks the current version of the database and each
new migration will be a new version, and any new migrations will run until the database is at the
current version.
Controllers- Characteristics
The above command creates a file called book_controller.rb in library’s app/controllers directory.
Open and see the file, the following snippet is found:
class BookController < ApplicationController
end
Routes
To define the routes for those actions which are defined as methods in the BookController class,
Open the routes.rb file in library/config/ directory and edit it with the following lines of code:
Rails.application.routes.draw do
get 'book/list'
get 'book/new'
post 'book/create'
patch 'book/update'
get 'book/list'
get 'book/show'
get 'book/edit'
get 'book/delete'
get 'book/update'
get 'book/show_subjects'
End
Routes
Use the following command to list all the defined routes, which are useful for tracking down routing
problems in the application.