5. What is Rails?
• Ruby on Rails is an extremely productive web
application framework written in Ruby by
David Heinemeier Hansson in 2005.
• You could develop a web application at least
ten times faster with Rails than you could
with a typical Java framework.
• An open source Ruby framework for
developing database-backed web
applications.
• No compilation phase required.
• Full-stack framework.
• Convention over Configuration
6. Full-stack framework.
•Includes everything needed to create a database-driven
web application, using the Model-View-Controller
pattern.
•Being a full-stack framework means all the layers are
built to work seamlessly together with less code.
•Requires fewer lines of code than other frameworks.
8. Convention over Configuration
•Rails shuns configuration files in favor of conventions,
reflection, and dynamic runtime extensions.
•Your application code and your running database
already contain everything that Rails needs to know!
12. App directory structure
• app - main app directory
• bin - rails needed scripts
• config - configurations for environment, routes,
etc.
• db - schema.db, migrations
• lib - folder for code not included in main app (like
your own gems)
• log - log files
• public - assets for public access, 404 page etc.
• test(spec) - test files
• tmp - place for temporary files
• vendor -folder used for third-party libraries.
13. Gemfile
A Gemfile is a file
which is used for
describing gem
dependencies for
Ruby programs.
What is Gemfile?
15. Development Workflow
• Use the rails command to create the basic skeleton of the application.
• Create a database on the MySQL server to hold your data.
• Configure the application to know where your database is located and the login
credentials for it.
• Create Rails Active Records (Models), because they are the business objects
you'll be working with in your controllers.
• Generate Migrations that simplify the creating and maintaining of database tables
and columns.
• Write Controller Code to put a life in your application.
• Create Views to present your data through User Interface. us start with creating
our library application.
17. ActiveRecord
• Rails Active Record is the Object/Relational Mapping (ORM) layer
supplied with Rails.
• Each Active Record object has CRUD methods for database access
18. Associations
Active Record supports three types of
associations:
• One-to-one (has_one);
• One-to-many (has_many);
• Many-to-many (has_and_belongs_to_many,
polymorphic);
19. Model in DB
• Each entity (such as book) gets a table in the database named after
it, but in the plural (books).
• Each such entity-matching table has a field called id, which contains a
unique integer for each record inserted into the table.
• Given entity x and entity y, if entity y belongs to entity x, then table y
has a field called x_id.
• The bulk of the fields in any table store the values for that entity's
simple properties (anything that's a number or a string).
20. Validations
The implementation of validations is done in a
Rails model. The data you are entering into the
database is defined in the actual Rails model, so it
only makes sense to define what valid data
entails in the same location. Also you can provide
custom validations.
21. Migrations
Rails Migration allows you to use Ruby to define
changes to your database schema, making it possible
to use a version control system to keep things
synchronized with the actual code.
23. Routes
The routing module
provides URL rewriting in
native Ruby. It's a way to
redirect incoming
requests to controllers
and actions. Best of all,
Rails' Routing works with
any web server.
Routes are defined in:
app/config/routes.rb
24. `
Rails named routes starts
with HTTP verb. It
supports all the verbs
such as: GET, POST,
PUTPATCH, DELETE.
25. Routes
Also rails have a special
route for root path.
You can match any url
to any action.
28. Controller
The Rails controller is the
logical center of your
application. It coordinates the
interaction between the user,
the views, and the model.
31. Strong params
It provides an interface for
protecting attributes from end-
user assignment. This makes
Action Controller parameters
forbidden to be used in Active
Model mass assignment until
they have been whitelisted.
32. Action View
In Rails, web requests are handled by Action Controller and Action View. Typically,
Action Controller is concerned with communicating with the database and performing
CRUD actions where necessary. Action View is then responsible for compiling the
response.
Action View templates are written using embedded Ruby in tags mingled with HTML.
34. Helpers
The Rails framework provides a
large number of helpers for
working with assets, dates,
forms, numbers and model
objects, to name a few. These
helpers are available to all
templates by default.
In addition to using the
standard template helpers
provided, creating custom
helpers to extract complicated
logic or reusable functionality
is strongly encouraged.
41. Homework
Write a simple web app which allows you to create an
event(bbq, pizza-day, or etc.). Add credentials to it (place, date,
etc…). Add two roles, admin, and attendee. Admin can add an
event, delete event, add a description, edit an event, add
comments to the event. The attendee can join an event, can
leave an event, can add comments to the event. You will need to
cover it by your preferred test framework and deploy to Heroku.