Intro to Rails Flashcards
MVC(Model, View, Controller):
- The _______ way to create user-interactive applications
- Created in 1979 by Trygve Reenskaug
- The default way to create user-interactive applications
- Created in 1979 by Trygve Reenskaug
What does the model do?
–Keeps track of ____ (session and longer)
–________, often via _________
–Enforces _________
–Keeps track of state (session and longer)
–Persistence, often via database
–Enforces consistency
What does View do?
•View ________ user interface
–Based on data in ______
–May look _______ based on browser, user role, assistive technologies, etc.
•View generates user interface
–Based on data in model
–May look different based on browser, user role, assistive technologies, etc.
What does Controllers do?
•Controllers _______
–Receives______ from ______
–Interacts with _____
•Controllers coordinate
–Receives events from view
–Interacts with model
MVC Picture:
•DB shouldn’t worry about how data are ________
–________ technology can change without affecting any other part of app
–Can add ______ , etc.
•DB shouldn’t worry about how data are displayed
–Display technology can change without affecting any other part of app
–Can add JavaScript, etc.
What is an important concept in MVC?
separation of _______
Important concept: separation of concerns
Business logic in only ___ _____
•Business logic in only one place
Separation of Concerns:
- Code/text only __________ one aspect of app
- Easier to ____, ______, ______, _______
- Rails essentially enforces ___
- Code/text only addresses one aspect of app
- Easier to write, maintain, document, debug
- Rails essentially enforces MVC
Rails concepts: Continuous Integration (CI)?
is a development practice that requires developers to ________ code into a shared _________ several times a day. Each check-in is then verified by an __________ build, allowing teams to detect problems early.
is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
Rails concepts: Extreme programming (XP)
is a software development _________ which is intended to improve software quality and _____________to changing customer requirements.
is a software development methodology which is intended to improve software quality and responsiveness to changing customer requirements.
Rails conepts: Kanban
is a ______ to control the ______ chain from a production point of view, and is an ___________ control system. Kanban was developed by Taiichi Ohno, an industrial engineer at Toyota, as a system to improve and maintain a high level of production. Kanban is one method to achieve ___.
is a system to control the logistical chain from a production point of view, and is an inventory control system. Kanban was developed by Taiichi Ohno, an industrial engineer at Toyota, as a system to improve and maintain a high level of production. Kanban is one method to achieve JIT.
Rails concepts: just-in-time (JIT) compilation:
also known as dynamic __________, is _________ done during execution of a program – __ ___ ____ – rather than prior to execution
also known as dynamic translation, is compilation done during execution of a program – at run time – rather than prior to execution.
Rails Concepts: Scrum:
is an ______ and __________ agile software development ____________ for managing product development.
is an iterative and incremental agile software development methodology for managing product development.
Rails concepts: Test-driven development (TDD)
is a software development process that relies on the ________ of a very short development cycle: first the developer ______ an (initially failing) automated test case that defines a desired _________or new _____, then produces the minimum amount of code to pass that test, and finally _______the new code to ________ standards.
is a software development process that relies on the repetition of a very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function, then produces the minimum amount of code to pass that test, and finally refactors the new code to acceptable standards.
Rails conventions: Requests are sent to a _______
–Sends request to an action in a _______
•Requests are sent to a router
–Sends request to an action in a controller
Rails conventions: •HTTP requests: ___, ____, ___, ______
HTTP requests: GET, POST, PUT, DELETE
Rails Concepts: Convention over configuration:
–Directory ________ mirrors MVC
–Don’t have to change _____ files much, if at all
•Don’t Repeat Yourself
•Agile
–Directory structure mirrors MVC
–Don’t have to change config files much, if at all
•Don’t Repeat Yourself
•Agile
Model Conventions: OO programming languages are designed around ______
•OO programming languages are designed around objects
Rails conventions: Router usually treats first part of _____ as the controller name:
•Router usually treats first part of path as the controller name
Model Conventions: Databases are designed around ______, ____, and ______
•Databases are designed around tables, rows, and columns
Rails conventions: •If action is _____, calls “create” method for that controller, via naming conventions
•If action is POST, calls “create” method for that controller, via naming conventions
ORM:Classes become ______
Classes become tables
ORM:Values in those objects become _______
Values in those objects become columns
ORM: Objects become ____ in those _____
Objects become rows in those tables
Model Naming Conventions: ______ are singular and table is ___
•Classes are singular and table is pural
Model Conventions: •How can we save objects in a __?
•How can we save objects in a DB?
ORM:
Object-______ ______ (ORM) is a technique (a.k.a. design pattern) of accessing a relational ________ from an ______-_______ language (Java, for example)
Object-relational mapping (ORM) is a technique (a.k.a. design pattern) of accessing a relational database from an object-oriented language (Java, for example)
ORM: Rails has ActiveRecord to do this, and much more
- ___, ______, ____, ______, etc.
•Rails has ActiveRecord to do this, and much more
–Add, delete, find, modify, etc.
ORM: Upside: stay within ____ and _____, no SQL
•Upside: stay within Ruby and Rails, no SQL
Views: Rails has XML ____
•XML builder
ORM Continued:
- Class ______ perform________ operations
- Instance _______ perform ______ operations
- Class methods perform table-level operations
- Instance methods perform row-level operations
Views: •ERb templates
–Allows ________ of Ruby in a document
–Somewhat violates __________ of concerns
•ERb templates
–Allows embedding of Ruby in a document
–Somewhat violates separation of concerns
Controllers:
•ActionPack contains ______ for ____ and ______
•ActionPack contains methods for views and controllers
Controllers: _______ sessions
Manages sessions
Controllers: Manages _______
•Manages caching
Controllers: Responsible for _______
Responsible for routing
CamelCase: CamelCase (also camel caps or medial capitals) is the practice of writing compound _____ or ________ such that each _____ or ______begins with a ______ letter.
CamelCase (also camel caps or medial capitals) is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter.
Naming Conventions:
•One major part of conventions is ______
–In most applications, _____ are meaningless to the _____
–In _____, ____ drive much of the ______
- Creating a ____ for a Person creates a _____ table people
- Variables ________ and ________
- ______are CamelCased
•One major part of conventions is naming
–In most applications, names are meaningless to the software
–In Rails, names drive much of the functionality
- Creating a model for a Person creates a database table people
- Variables lowercase and underscores
- Classes are CamelCased
Naming Conventions Continued:
•DB table names “___ ____” class names
–Always ____
- With ______ and __________
- Class Student
–DB table _______
–app/models/students.rb
•DB table names “look like” class names
–Always plural
- With lowercase and underscores
- Class Student
–DB table students
–app/models/students.rb
Naming Conventions Continued Controllers:•Controllers
–StudentController
–app/controllers/student_controller.rb
–http://…/student/list
- Calls ___ method in student_controller.rb
- Subdirectories add to ____
–View _______ in app/views/student
–Layout _______ app/views/layouts/application.html.erb and .xml
•Controllers
–StudentController
–app/controllers/student_controller.rb
–http://…/student/list
- Calls list method in student_controller.rb
- Subdirectories add to path
–View templates in app/views/student
–Layout templates app/views/layouts/application.html.erb and .xml
New:
$ rails ___ demo
create
create README.rdoc
create Rakefile
…
Your bundle is complete! Use bundle show [gemname]
to see where a bundled gem is installed.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data –install
= 1.9.1 : gem install rdoc-data; rdoc-data –install
>= 1.9.2 : nothing to do! Yay!
$ rails new demo
create
create README.rdoc
create Rakefile
…
Your bundle is complete! Use bundle show [gemname]
to see where a bundled gem is installed.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data –install
= 1.9.1 : gem install rdoc-data; rdoc-data –install
>= 1.9.2 : nothing to do! Yay!
“Conventions”:
•create config/routes.rb
–Message à method _______
–See via “___ _____”
•create config/________.rb
–Timezone, locale, encodings, preloads, etc.
- create config/environment.rb
- create config/environments
•create config/routes.rb
–Message à method mapping
–See via “rake routes”
•create config/application.rb
–Timezone, locale, encodings, preloads, etc.
- create config/environment.rb
- create config/environments
Three Modes:
- rails server –e development
- create config/environments/development.rb
–No ______, etc.
- create config/environments/production.rb
- create config/environments/test.rb
•
- rails server –e development
- create config/environments/development.rb
–No caching, etc.
- create config/environments/production.rb
- create config/environments/test.rb
•
“Conventions” Continued
- create config/locales
- create config/locales/en.yml
- create config/boot.rb
- create config/database.yml
–Which DB to ___ and how to ______
- create config/locales
- create config/locales/en.yml
- create config/boot.rb
- create config/database.yml
–Which DB to use and how to connect
Default Messages:
- create public/404.html
- create public/422.html
- create public/500.html
- create public/favicon.ico
- create public/index.html
- create public/robots.txt
- create public/404.html
- create public/422.html
- create public/500.html
- create public/favicon.ico
- create public/index.html
- create public/robots.txt
Default Tests
- create test/fixtures
- create test/functional
- create test/integration
- create test/unit
- create test/performance/browsing_test.rb
- create test/test_helper.rb
- create test/fixtures
- create test/functional
- create test/integration
- create test/unit
- create test/performance/browsing_test.rb
- create test/test_helper.rb
App:
•___/controllers:
–application_controller.rb
•app/____:
–application_helper.rb
- app/______
- app/models
- app/views/layouts:
–_________.html.erb
•app/controllers:
–application_controller.rb
•app/helpers:
–application_helper.rb
- app/mailers
- app/models
- app/views/layouts:
–application.html.erb
Aside: Errorr:
$ rails generate ______ Student studentname:string studentid:integer
______ active_record
The name ‘Student’ is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
$ rails generate scaffold Student studentname:string studentid:integer
invoke active_record
The name ‘Student’ is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
Scaffolding: A scaffold in Rails is a full __ __ _____, __________ migration for that _____, controller to manipulate it, _____ to view and manipulate the data, and a ____ _____ for each of the above.
•A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
Types:
- :datetime
- :timestamp
- :time
- :date
- :binary
- :boolean
- :primary_key
- :string
- :text (short strings)
- :integer
- :float
- :decimal (fixed precision, currency)
- :datetime
- :timestamp
- :time
- :date
- :binary
- :boolean
- :primary_key
- :string
- :text (short strings)
- :integer
- :float
- :decimal (fixed precision, currency)
Create a Student:
$ rails new demo
$ cd demo
$ rails generate _____ Student studentname:string studentid:integer
$ rails new demo
$ cd demo
$ rails generate scaffold Student studentname:string studentid:integer
Migration:
class CreateStudents < ____________::______
def change
create_table :students do |t|
t. _____ :studentname
t. ______:studentid
_.timestamps null: false
end
end
end
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t. string :studentname
t. integer :studentid
t.timestamps null: false
end
end
end
Aside: Errorr:
$ rails ___ Student
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
[…]
$ rails new Student
create
create README.rdoc
create Rakefile
create config.ru
create .gitignore
create Gemfile
[…]
rake db:migrate
$ rake db:migrate
== CreateStudents:________
=================================================
– create_table(:students)
-> 0.0014s
== CreateStudents: migrated (0.0015s) ========================================
$ rake db:migrate
== CreateStudents: migrating =================================================
– create_table(:students)
-> 0.0014s
== CreateStudents: migrated (0.0015s) ========================================
If you don’t like it:
$ rails destroy______ Student
$ rails destroy scaffold Student
If you really don’t like it:
$ spring ___
$ cd ..
$ rm –rf college
$ spring stop
$ cd ..
$ rm –rf college
rake routes:
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
students GET /students(.:format) students#index
POST /students(.:format) students#create
new_student GET /students/new(.:format) students#new
edit_student GET /students/:id/edit(.:format) students#edit
student GET /students/:id(.:format) students#show
PUT /students/:id(.:format) students#update
DELETE /students/:id(.:format) students#destroy
The Entire Model
$ more app/models/student.rb
class Student < ActiveRecord::Base
attr_______ :id, :name
end
$ more app/models/student.rb
class Student < ActiveRecord::Base
attr_accessible :id, :name
end
Controller: Find All
class StudentsController < __________
GET /students
GET /students.json
def index
@students = Student.all
respond_to do |format|
format. html # _____ .html.erb
format. json { render json: @students }
end
end
class StudentsController < ApplicationController
GET /students
GET /students.json
def index
@students = Student.all
respond_to do |format|
format. html # index.html.erb
format. json { render json: @students }
end
end
Controller: Find One
GET /students/1
GET /students/1.json
def show
@student = Student. ___(params[:id])
_______ do |format|
format. html # ____ .html.erb
format. json { render json: @student }
end
end
•
GET /students/1
GET /students/1.json
def show
@student = Student.find(params[:id])
respond_to do |format|
format. html # show.html.erb
format. json { render json: @student }
end
end
•
Controller: New
GET /students/new
GET /students/new.json
def new
@student = Student. ___
respond_to do |format|
format. html # new.html.erb
format. json { render json: @student }
end
end
GET /students/new
GET /students/new.json
def new
@student = Student.new
respond_to do |format|
format. html # new.html.erb
format. json { render json: @student }
end
end
Controller: Edit
GET /students/1/edit
def ____
@student = Student.find(params[:id])
end
automagically calls app/views/students/edit.html.erb
GET /students/1/edit
def edit
@student = Student.find(params[:id])
end
automagically calls app/views/students/edit.html.erb
Controller: Create
POST /students
POST /students.json
def create
@student = Student.___(params[:student])
respond_to do |format|
if @student.____
format. html { redirect_to @student, notice: ‘Student was successfully created.’ }
format. json { render json: @student, status: :created, location: @student }
else
format. html { render action: “new” }
format. json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
POST /students
POST /students.json
def create
@student = Student.new(params[:student])
respond_to do |format|
if @student.save
format. html { redirect_to @student, notice: ‘Student was successfully created.’ }
format. json { render json: @student, status: :created, location: @student }
else
format. html { render action: “new” }
format. json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
Controller: Update:
PUT /students/1
PUT /students/1.json
def _____
@student = Student.find(params[:id])
respond_to do |format|
if @student.update_attributes(params[:student])
format. html { redirect_to @student, notice: ‘Student was successfully updated.’ }
format. json { head :no_content }
else
format. html { render action: “edit” }
format. json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
PUT /students/1
PUT /students/1.json
def update
@student = Student.find(params[:id])
respond_to do |format|
if @student.update_attributes(params[:student])
format. html { redirect_to @student, notice: ‘Student was successfully updated.’ }
format. json { head :no_content }
else
format. html { render action: “edit” }
format. json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
Controller: Delete/Destroy
DELETE /students/1
DELETE /students/1.json
def _______
@student = Student.find(params[:id])
@student.destroy
respond_to do |format|
format. html { redirect_to students_url }
format. json { head :no_content }
end
end
end
DELETE /students/1
DELETE /students/1.json
def destroy
@student = Student.find(params[:id])
@student.destroy
respond_to do |format|
format. html { redirect_to students_url }
format. json { head :no_content }
end
end
end
View: Index
Name Id
View: Index Continued
Partial:
Partial Continued:
View: New
View: Edit
Model Naming Conventions: Classes are _____ ______ , Rails turns them into _____ ______ and ________
•Classes are camel cased, Rails turns them into lower cased and underscored
Model Naming Conventions:
•Classes are camel cased, Rails turns them into ____ _____ and ______________
–Class StudentEmployee creates a table student_employees
•Classes are camel cased, Rails turns them into lower cased and underscored
–Class StudentEmployee creates a table student_employees
Controller Naming Convention
•If there is a ClassScheduler ___________
–Creates app/controllers/class_scheduler.rb
•If there is a ClassScheduler controller
–Creates app/controllers/class_scheduler.rb
View Naming Convention:
- app/helpers/class_scheduler.rb
- ERb _________ in app/view/class_scheduler
- app/helpers/class_scheduler.rb
- ERb templates in app/view/class_scheduler
Route Naming: If a _______ arrives for “admin/book”, _____ looks for a book_controller.rb in app/controllers/admin
•If a request arrives for “admin/book”, Rails looks for a book_controller.rb in app/controllers/admin
ActiveRecord Automatic Additions:
•id – default primary key
–Can ______, but still reference via “id
•id – default primary key
–Can override, but still reference via “id
ActiveRecord Automatic Additions:
•x_id – _______ key for a table named x
•x_id – foreign key for a table named x
ActiveRecord Automatic Additions:
•created_at, created_on, updated_at, updated_on
–_at have a ______
•created_at, created_on, updated_at, updated_on
–_at have a time