Upload
ngoliem
View
216
Download
0
Embed Size (px)
Citation preview
Assumptions
• Passing familiarity with Ruby or equivalent dynamic language fundamentals (duck-typing, run-time object alteration, etc.)
• Knowledge of object-oriented programming (OOP)
• Attention span >= gnat
3
Objectives
• Learn the basics of a Rails application
• Learn the basics of “Convention over Configuration”
• Learn the basics of the Model-View-Controller (MVC) architecture
• Learn the basics of a RESTful application/service
• Understand how Rails templates and views work
4
What We Won’t Cover
• Ruby syntax and conventions
• Ruby API
• Rails framework API/CLI/Gemsets
• Rails Middleware and Rack
• Advanced environment configuration
• Database adapters, querying, and management
• Design Patterns
5
What is “Rails”?
• Rails is a web application framework built upon, and written in, the Ruby programming language.
• Open source
• Easy to learn; difficult to master.
• Fun (and a time-saver)!
• Utilizes a Model-View-Controller (MVC) architecture (more on that in a moment).
7
Principles
• Rails operates under many principles, but here are my top two:
• DRY - “Don’t Repeat Yourself”
• Use proper abstraction for implementations to avoid redundancy (e.g. presenters and decorators)
• CoC - “Convention over Configuration”
• Many things are configured automatically by virtue of file structures and class names
9
CoC
• This constitutes a vast majority of any Rails app.
• The most obvious form of CoC is seen in the models, views, controllers, and database schema.
• To wit: scaffolding and generators.
11
What is MVC?
• MVC is the architecture upon which Rails operates.
• It breaks down the web application into three distinct (but not necessarily discrete) entities:
• The models (objects)
• The views (visual interfaces)
• The controllers (communication between models and views)
13
MVC: How it Works
1. A user visits a site and makes a request.
2. The dispatcher routes the user’s request to the appropriate controller.
3. The controller interacts with the models in the application to fetch, alter, create, or store data within the database based on the user’s input/request.
4. The view requested by the user is passed back to the controller and rendered in their browser, which contains the information from the models.
5. Rinse and repeat.
15
MVC: Model
• Models are classes. They represent objects used by the Rails application.
• Model names are often linked directly to a table name in a database (line 1).
fig 1: basic rails model with validators and relationships
16
MVC: Model (cont)
• Basic anatomy of a model:
• ...validations for data used to populate the database (line 3).
• ...associations with other models (lines 5-6).
• ...user-defined instance methods (not shown).
fig 1: basic rails model with validators and relationships
17
MVC: Controller
• Acts as an intermediary between the models and the views.
• Responds to user interactions from the user interface and performs operations on the models.
• Each action in a controller is [typically] linked to a default template in the Rails application.
fig 3: basic rails controller with default actions
18
MVC: Controller (cont)
• Controllers should include all RESTful actions by default until you know better.
• Actions can be omitted if necessary.
• Controllers can include non-RESTful actions.
• They can also respond to multiple formats (XML, JSON, HTML)
fig 3: basic rails controller with default actions
19
MVC: View
• The actual user interface of the application.
• Used to display and interact with data from the database.
• Default file format: HTML ERB (HTML with embedded Ruby)
fig 3: basic Rails view in “erb” format
21
MVC: View (cont)
• Views come in many (default) flavors:
• “*.html” - Standard HTML
• “*.erb” - Embedded Ruby
• “*.html.erb” - HTML with Embedded Ruby
• “_*.html.erb” - A partial (note the leading underscore)
• “*.json” - JSON-encoded string representing one or more models
22
MVC: View (JSON)
• JSON views may be:
• Visual - RSS feeds and standard HTML (tabular data, etc)
• Or non-visual - Application API
• They have other advantages as well:
• Rails applications can use JSON views to serve the main website, thereby eliminating the need for two sets of controllers (one web-based, another for API requests)
23
MVC: View (cont)
• The main type of view in a Rails application is a “template”
• Templates usually map directly to an associated controller action
• “New”, “Edit”, “Show”, or “Index”
• Another type of view is called a “partial”
• These are reusable view snippets
24
MVC: View (do’s and don’ts)
• Inline CSS styling
• Inline template style blocks (“<style></style>”)
• Inline Javascript
• Inline template Javascript blocks (“<script></script>”)
29
Words of Wisdom
• Work smart: if something simple is taking too long, there is likely a better way.
• ASK QUESTIONS - if you do not understand, say so (it saves time and effort).
• TEST YOUR CODE - never, ever, submit a feature or patch without thoroughly testing it. Write automated tests or test by hand.
32
Just in Case...
• DID I MENTION THAT YOU SHOULD ASK QUESTIONS?
• HERE’S A SLIDE DEDICATED TO THAT, JUST IN CASE THE POINT WAS LOST OR MISSED.
• ASK QUESTIONS.
33
Resources
• Rails Guides (guides.rubyonrails.org)
• Rails API (api.rubyonrails.org)
• Railscasts (railscasts.com - “pro” videos require a subscription, sold separately)
• Stack Overflow (stackoverflow.com)
• Yourself, your friends, instructors, advisors, people like me
34
Resources (environment)
• Sublime text (editor, not an IDE)
• Rubymine (IDE, resource-intensive, not free)
• Rails Console (“rails c” on command-line)
• Web inspection tools
35