3. Getting started
1. Ruby on Rails command
2. Generate an article scaffold
• MVC pattern
3. Synchornize database schema
• ORM
4. Modify scaffold
4. Ruby on Rails Command
rails generate scaffold scaffold_name field1_name:field1_type
field2_name:field2_type ... fieldN_name:fieldN_type
• Replace scaffold_name with actual scaffold names
• Replace field1_name to fieldN_name with actual field
names
5. Ruby on Rails command (Cont.)
field1_type to fieldN_type needs to be a valid data type
source: https://ihower.tw/rails4/migrations.html#section
6. Generate an article scaffold
rails generate scaffold article title:string body:text
The command generates...
• a controller handles requests from clients and transfer data
between models and views
• a model with one string field called title and one text field
body called article
• a form for create and update articles and several views to
list articles and to display single article
9. Models and views DO NOT
directly exchange data
Remember this till the Apocalypse
10. Synchronize database schema
rake db:migrate
• rake runs scripts called Rakefile. Rakefile holds a bundle
of commands related to Ruby on Rails but not part of Ruby
on Rails
• db:migrate: synchronize database schema, as known as
database migration
• tmp:clear: clean temporary files
15. ORM (Cont.)
• Object Relational Mapping
• Object stands for objects in Ruby on Rails
• Relational stands for relational database system, such as
MySQL, PostgreSQL, Microsoft SQL Server...etc.
• Mapping stands for the procedure transfer data structure
into table row
16. Start web server
If you forget how to do so,
feel free to take a look on #101
20. Add a field
1. Modify model
1. Create a database migration
2. Synchronize database schema
2. Modify the controller
3. Modify views
21. Create a database migration
rails generate migration
add_author_to_articles author:string
• Replace author with field name you want to add
• Replace articles with plural form of model
• Replace string with valid data type from the table
mentioned before
23. Modify the controller
# Only allow a trusted parameter "white list" through.
def article_params
params.require(:article).permit(:title, :body, :author)
end
• Append :author to the list of white list
43. Controller (Cont.)
1. One controller has many actions
2. Each action has its own purpose
3. Actions are isolated to each other
4. One action takes care of one request
44. Model
1. Query rows from database
2. Process data
3. Write data into database
46. Model (Cont.)
1. Model DOES NOT hold fields, schema DOES
2. Model is a class
3. In ORM, fields would be mapped as properties in object,
thus we can manipulate them via methods
47. Model (Cont.)
• HumanBeing : Model
• Person : Object
• People : Iterable object (a.k.a. array) of objects
51. Views (Cont.)
1. Symbol @ points to corresponding controller in views
2. DO NOT conduct complicated calculations or property
access in views
3. Views should have only if and each statements
52. Homework
1. Create a scaffold from scratch
2. Add a field to the scaffold
3. Remove a existing field from the scaffold