1 dr alexiei dingli web science stream introducing rails
TRANSCRIPT
![Page 1: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/1.jpg)
1
Dr Alexiei Dingli
Web Science Stream
Introducing Rails
![Page 2: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/2.jpg)
2
• Open source web application framework for Ruby
• Intended for agile programming
• Create by David Heinemeier Hansson
• Released originally in 2004 but only became popular 2007 when it was distributed with Mac OS X Leopard
What is Rails?
![Page 3: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/3.jpg)
3
• Development
• Testing
• Production
3 Environments
![Page 4: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/4.jpg)
4
• Changes to source code immediately visible (just reload)
• Speed is not critical
• Provide insight to developer– Errors are easily spotted by showing the stack
trace
Development
![Page 5: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/5.jpg)
5
• Fill database using dummy data
• Unit and functional testing are fully automated
• Invoked from the command line
Testing
![Page 6: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/6.jpg)
6
• Update to code base infrequent
• Production environment can be optimised
• Focus on performance
• Errors can be sent by email to the developer
Production
![Page 7: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/7.jpg)
7
/config/Database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
timeout: 5000
production:
adapter: mysql
database: newMySqlProductionDB
username: root
password: superSecretPassword
timeout: 5000
Setting our database
![Page 8: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/8.jpg)
8
Database Server
Development
Table 1 Table 2
Test
Table 1 Table 2
Production
Table 1 Table 2
Database architecture
![Page 9: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/9.jpg)
9
• Models– Handling data and business logic
• Controllers– Handling the user interface and application
logic
• Views– Handling GUI and persistent objects
Model-View-Controller Architecture
![Page 10: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/10.jpg)
10
MVC Process
![Page 11: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/11.jpg)
11
• Improves scalability– Upgrade parts of the system without changing other
components (Eg just the database)
• Makes maintenance easier– Components have low dependency on each other
• Promotes reuse– Model may be reused by multiple views (or vice-
versa)
Why MVC?
![Page 12: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/12.jpg)
12
app
controllers
helpers
models
views
MVC in Rails
![Page 13: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/13.jpg)
13
• Called Active Records– Connect to database– Retrieve data from tables– Store data in tables
• Abstracts from the database– Can connect immediately to SQLite, MySQL
and PostgreSQL– RubyGems provide access to other
Databases
The model
![Page 14: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/14.jpg)
14
• Active Records abstract database specific code thus making it easy to shift between different databases
• Another magic of Rails is migration which saves us from creating any SQL
Database Abstraction
![Page 15: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/15.jpg)
15
• Active Controller– Is the glue between the application data,
presentation layer and the web browser– Decides how to handle a request– Retrieve data from the model to be passed– Gather information from browser request
The controller
![Page 16: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/16.jpg)
16
• Class names should be written with each word beginning with a capital letter and no spaces between words– StoryBook– AgeCalculator
• Filenames are written in lowercase with undderscores separating each word– story_book– age_calculator
Some conventions
![Page 17: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/17.jpg)
17
• Action View– Should only contain presentation logic– Can contain HTML and also Ruby code using
the embded Ruby (ERb) syntax
<b><%= “Hello World!” %></b>
The View
![Page 18: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/18.jpg)
18
• The template has a one-to-one mapping to the action of a controller– Add should be in Add.html.erb
• The folder that stores the template should be named after the controller
Views conventions
![Page 19: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/19.jpg)
19
html.erb
Standard HTML template which might contan ERb tags
xml.builder
Output XML (Eg RSS feeds)
js.rjs
Return JavaScript instructions
Views extensions
![Page 20: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/20.jpg)
20
• REpresentational State Transfer – Design pattern not restricted to the WWW– Offers a resource centric approach where
• Client uses a resource• Every resource needs to be addressed uniquely• Interactions with resources are exposed as
operations• Most common are the CRUD operations
– Create, Read, Update, Delete
Lets have some REST
![Page 21: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/21.jpg)
21
• The protocol used is– Stateless
• Each request to the server is completely independent
– Cacheable• Proxies
– Layered• Routers• Firewalls
More REST
![Page 22: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/22.jpg)
22
CRUD HTTP
CREATE POST
READ GET
UPDATE PUT (Not implemented)
DELETE DELETE (Not implemented)
RESTing on the WWW
![Page 23: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/23.jpg)
23
• Deep relationship to provide assistance for easy construction of URLs
• Rails uses modified POST requests to implement the PUT and DELETE methods
The REST of Rails
![Page 24: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/24.jpg)
24
• Rails generate the application’s structure easily
• Simply use the generate script
• Try – ruby script/generate
Magical Generation
![Page 25: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/25.jpg)
25
• Convention over configuration– Developers only specify unconventional
aspects– Eg if we have a class Sale in the model, the
table will be called sales automatically– Leads to less code and less repetition– If the convention is not kept then more
programming is needed
Rails Philosophy (1)
![Page 26: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/26.jpg)
26
• Don’t Repeat Yourself (DRY)– Information located in a single unambiguous
place– Eg using Active Records, no columns or
tables are specified, everything is done automatically.
Rails Philosophy (2)
![Page 27: 1 Dr Alexiei Dingli Web Science Stream Introducing Rails](https://reader035.vdocuments.mx/reader035/viewer/2022070415/56649e985503460f94b9b5ab/html5/thumbnails/27.jpg)
27
Questions?