Intro To Ruby On Rails
Intro To Ruby On Rails
learn-rails.com
Contents
1.
2.
Chapter 1
About RailsApps
The best way to learn is by doing; when it comes to code, that means building applications.
The RailsApps project provides open source example applications for Rails developers, for
free. Our work is supported by subscribers who want access to our in-depth tutorials.
Our tutorials take you on a guided path starting with absolute basics. With each tutorial you
will gain knowledge as you build a real-world Rails application. As you build, youll feel
genuine satisfaction at each step. Hands-on learning with real Rails applications is the key to
absorbing and retaining knowledge.
Monthly Subscription
The RailsApps project is supported by a monthly subscription. When you subscribe, you get:
the book Learn Ruby on Rails
the book Rails and Bootstrap
additional beginner and intermediate-level tutorials
In addition youll support development of:
new example applications
more tutorials
tools to create starter applications
a growing community resource
I invite you to join the RailsApps project and support our work for the Rails community.
You can join the RailsApps project and get access to all the tutorials for only $19 per month.
Youll need a credit card to sign up. Your subscription starts immediately and you can cancel
anytime.
For your subscription, sign up here:
Join RailsApps
Thank you! I appreciate your support for our work.
Chapter 2
Concepts
This chapter provides the background, or big picture, you will need to understand Rails.
This chapter is excerpted from the book Learn Ruby on Rails by Daniel Kehoe. Read the book
for a complete introduction.
Here are the key concepts youll need to know before you try to use Rails.
Static websites are ideal for particle physics papers (which was the original use of the World
Wide Web). But most sites on the web, especially those that allow a user to sign in, post
comments, or order products and services, generate web pages dynamically.
Dynamic websites often combine web pages with information from a database. A database
stores information such as a users name, comments, Facebook likes, advertisements, or any
other repetitive, structured data. A database query can provide a selection of data that
customizes a webpage for a particular user or changes the web page so it varies with each
visit.
Dynamic websites use a programming language such as Ruby to assemble HTML, CSS, and
JavaScript files on the fly from component files or a database. A software program written in
Ruby and organized using the Rails development framework is a Rails web application. A web
server program that runs Rails applications to generate dynamic web pages is an application
server (but usually we just call it a web server).
Software such as Rails can access a database, combining the results of a database query with
static content to be delivered to a web browser as HTML, CSS, and JavaScript files. Keep in
mind that the web browser only receives ordinary HTML, CSS, and JavaScript files; the files
themselves are assembled dynamically by the Rails application running on the server.
Even if you are not going to use a database, there are other good reasons to generate a
website using a programming language. For example, if you are creating several web pages,
it often makes sense to assemble an HTML file from smaller components. For example, you
might make a small file that will be included on every page to make a footer (Rails calls these
partials). Just as importantly, if you are using Rails, you can add features to your website
with code that has been developed and tested by other people so you dont have to build
everything yourself.
The widespread practice of sharing code with other developers for free, and collaborating
with strangers to build applications or tools, is known as open source software development.
Rails is at the heart of a vibrant open source development community, which means you
leverage the work of tens of thousands of skilled developers when you build a Rails
application. When Ruby code is packaged up for others to share, the package is called a gem.
The name is apt because shared code is valuable.
Ruby is a programming language; Rails is a development framework. That means Rails is a
set of structures and conventions for building a web application using the Ruby language. Rails
is also a library or collection of gems that developers use as the core of any Rails web
application. By using Rails, you get well-tested code that implements many of the mostneeded features of a dynamic website.
With Rails, you will be using shared standard practices that make it easier to collaborate
with others and maintain your application. As an example, consider the code that is used to
access a database. Using Ruby without the Rails framework, or using another language such
as PHP, you could mix the complex programming code that accesses the database with the
code that generates HTML. With the insight of years of developers collective experience in
maintaining and debugging such code, Rails provides a library of code that segregates
database access from the code that displays pages, enforcing separation of concerns, and
making more modular, maintainable programs.
In a nutshell, thats how the web works, and why Rails is useful.
For a history of Rails, and an explanation of why it is popular, see the article What is Ruby
on Rails?
What is Rails?
So far, Ive defined Rails in two ways: as structures and conventions for building a web
application, and as a library or collection of code.
To really understand Rails, and succeed in building Rails applications, we need to consider
Rails from six other perspectives. Like six blind men encountering an elephant, it can be
difficult to understand Rails unless you look at it from multiple points of view.
Here are six different ways of looking at Rails, summarized from the article What is Ruby on
Rails?
sync with a remote GitHub repository, making it possible to collaborate with others on open
source or proprietary projects. Strictly speaking, Git and GitHub are not part of Rails (they
are tools that can be used on any development project). And there are several other version
control systems that are used in open source development. But a professional Rails
developer uses Git and GitHub constantly on any real-world Rails project.
Finally, we can consider a Rails application from the perspective of a tester
tester. Software testing
is part of Rails culture; Rails is the first web development platform to make testing an
integrated part of development. Before Rails, automated testing was rarely part of web
development. A web application would be tested by users and (maybe) a QA team. If
automated tests were used, the tests were often written after the web application was largely
complete. Rails introduced the discipline of Test-Driven Development (TDD) to the wider
web development community. With TDD, tests are written before any implementation
coding. It may seem odd to write tests first, but for a skilled TDD practitioner, it brings
coherence to the programming process. First, the developer will give thought to what needs
to be accomplished and think through alternatives and edge cases. Second, the developer
will have complete test coverage for the project. With good test coverage, it is easier to
refactor, rearranging code to be more elegant or efficient. Running a test suite after
refactoring provides assurance that nothing inadvertently broke after the changes.
TDD is seen as a necessary skill of an experienced Rails developer. Because this is a tutorial
for beginners, it will not introduce you to techniques of Test-Driven Development. As you
work through more advanced tutorials, youll be introduced to Test-Driven Development.
Stacks
To understand Rails from the perspective of a professional Rails developer, youll need to
grasp the idea of a technology stack and recognize that Rails can have more than one stack.
A technology stack is a set of technologies or software libraries that are used to develop an
application or deliver web pages. Stack is a term that is used loosely and descriptively.
There is no organization that sets the rules about what goes into a stack. As a technologist,
your choice of stack reflects your experience, values, and personal preference, just like
religion or favorite beverage.
For example, Mark Zuckerberg developed Facebook in 2004 using the LAMP application
stack:
Linux (operating system)
Apache (web server)
MySQL (database)
PHP (programming language)
For this tutorial, your application stack will be:
10