Image credits: RubyonRails.org
A Web Development Framework for Purists
Back in 1995, Yukihiro Matsumoto released Ruby 0.95 as a pure, object-oriented (OO), general-purpose programming language:
As a language maniac and OO fan for 15 years, I really wanted a genuine object-oriented, easy-to-use scripting language. I looked for but couldn't find one. So I decided to make it. — Y. Matsumoto
Then in 2004, David Heinemeier Hansson released the first version of Ruby on Rails as a web application framework which he used to launch his team management application Basecamp. He open sourced Ruby on Rails in 2005, and Apple released it with OS X Leopard in 2007.
For this tutorial, I'll refer to Ruby on Rails as Rails for short; some people also use RoR.
Rails is used by a wide variety of popular web applications you may know, such as GitHub, Shopify, Airbnb, Twitch, SoundCloud, Hulu, Zendesk, Square, and Highrise. However, it's been criticized for scalability limitations, most notably with Twitter, which gradually had to move many of its services to other platforms. That said, Twitter has high transaction rates and scalability challenges.
In this tutorial, I'll answer the question "What is Ruby on Rails?" and introduce you to the basics of the programming framework to help you experiment with it.
Before we get started, please remember, I participate in the discussions below. If you have a question or topic suggestion, please post a comment below or contact me on Twitter @reifman.
What Is Ruby on Rails?
Ruby on Rails is a popular, open-source, object-oriented web development framework used by many programmers and application providers. It's also based on a model view controller (MVC) approach.
Models map to the databases and functionality to an object in the application, such as users. Rails provides a standard convention for naming and file directory structures, which simplifies programming but also provides for automated solutions to building functionality, called scaffolding. It also ensures a common environment for developers to work together and benefit from each other's efforts.
Optimizing for programmer happiness with Convention over Configuration is how we roll.
The Controller manages requests primarily between the user and the server, gathering data from models and returning it to the user through View files.
Views are essentially programmatic HTML files that lay out the page with the dynamic data.
Rails conventions guide developers to rely on RESTful routing. Controller actions generally e.g. new, create, edit, update, destroy, show, index, etc. These are what allow for users and the system to interact with various features
Collectively, the MVC features of Rails are called the Action Pack, i.e. ActionController, ActionView and ActiveRecord, the latter for interacting with the database.
Rails' founder Hansson provides a principled theology behind the framework called The Ruby on Rails Doctrine.
The Ruby on Rails Doctrine
The Rails doctrine consists of eight tenets:
- Optimize for programmer happiness: whenever possible, Ruby attempts to meet the desires of the programmer.
- Convention over Configuration: choosing standard, platform-wide default choices makes it faster and easier for programmers to get started on new projects.
- The menu is omakase: the team behind Ruby on Rails has selected the best tools and approaches, and everyone will be happier using them.
- No one paradigm: in the end Rails embraces flexible solutions for various occasions.
- Exalt beautiful code: a primary focus on aesthetics and simplicity.
- Value integrated systems: an orientation towards complete awareness and cohesion of all the tools needed to build applications and solutions.
- Progress over stability: a willingness to take risks to move the platform forward.
- Push up a big tent: welcoming a big community of participants and their voices to set the vision.
The chief accomplishment of Rails was to unite and cultivate a strong tribe around a wide set of heretical thoughts about the nature of programming and programmers. — David Heinemeier Hansson
Here are the primary Rails contributors currently (founder David Hansson is in the upper left):
And they do have a warm, welcoming community:
- The Ruby on Rails: Talk mailing list
- The Ruby on Rails StackOverflow Q&A tag
- The #rubyonrails IRC channel on irc.freenode.net
- An annual RailsConf conference for real world meetups
Perhaps you're interested in diving in further, so here's how to get started.
Getting Started With Ruby on Rails
Rails has a collection of outstanding documentation. Here's a peek at the Getting Started Guide:
The guide steps you through a general introduction, basic installation and some beginner Rails programming.
Installing Ruby on Rails
Mac users will find Ruby pre-installed. Windows users can check out RailsInstaller. For a full Rails installation on OS X, the guide recommends Tokaido. Alternatively, Daniel Kehoe suggests avoiding one-click installers and provides detailed instructions for Mac OS X El Capitan install.
Experienced Rails developers also report that it makes web application development more fun. — Rails Guide
However, it had been a while since I'd run Rails, and reinstalling it on my late model Mac on El Capitan was time-consuming.
Verify the Installation of Ruby
On OS X, Ruby will be pre-installed:
$ ruby -v ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
Install the Rails Framework
My Rails installation took about 10 to 15 minutes (be patient):
$ sudo gem install rails Password: Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully installed tzinfo-1.2.2 Fetching: minitest-5.8.4.gem (100%) Successfully installed minitest-5.8.4 Fetching: activesupport-220.127.116.11.gem (100%) Successfully installed activesupport-18.104.22.168 Fetching: rack-1.6.4.gem (100%) Successfully installed rack-1.6.4 Fetching: rack-test-0.6.3.gem (100%) Successfully installed rack-test-0.6.3 Fetching: mini_portile2-2.0.0.gem (100%) Successfully installed mini_portile2-2.0.0 Fetching: nokogiri-22.214.171.124.gem (100%) Building native extensions. This could take a while... Successfully installed nokogiri-126.96.36.199 Fetching: loofah-2.0.3.gem (100%) Successfully installed loofah-2.0.3 Fetching: rails-html-sanitizer-1.0.3.gem (100%) Successfully installed rails-html-sanitizer-1.0.3 Fetching: rails-deprecated_sanitizer-1.0.3.gem (100%) Successfully installed rails-deprecated_sanitizer-1.0.3 Fetching: rails-dom-testing-1.0.7.gem (100%) Successfully installed rails-dom-testing-1.0.7 Fetching: builder-3.2.2.gem (100%) Successfully installed builder-3.2.2 Fetching: erubis-2.7.0.gem (100%) Successfully installed erubis-2.7.0 Fetching: actionview-188.8.131.52.gem (100%) Successfully installed actionview-184.108.40.206 Fetching: actionpack-220.127.116.11.gem (100%) Successfully installed actionpack-18.104.22.168 Fetching: activemodel-22.214.171.124.gem (100%) Successfully installed activemodel-126.96.36.199 Fetching: arel-6.0.3.gem (100%) Successfully installed arel-6.0.3 Fetching: activerecord-188.8.131.52.gem (100%) Successfully installed activerecord-184.108.40.206 Fetching: globalid-0.3.6.gem (100%) Successfully installed globalid-0.3.6 Fetching: activejob-220.127.116.11.gem (100%) Successfully installed activejob-18.104.22.168 Fetching: mime-types-2.99.gem (100%) Successfully installed mime-types-2.99 Fetching: mail-2.6.3.gem (100%) Successfully installed mail-2.6.3 Fetching: actionmailer-22.214.171.124.gem (100%) Successfully installed actionmailer-126.96.36.199 Fetching: thor-0.19.1.gem (100%) Successfully installed thor-0.19.1 Fetching: railties-188.8.131.52.gem (100%) Successfully installed railties-184.108.40.206 Fetching: bundler-1.11.2.gem (100%) Successfully installed bundler-1.11.2 Fetching: concurrent-ruby-1.0.0.gem (100%) Successfully installed concurrent-ruby-1.0.0 Fetching: sprockets-3.5.2.gem (100%) Successfully installed sprockets-3.5.2 Fetching: sprockets-rails-3.0.1.gem (100%) Successfully installed sprockets-rails-3.0.1 Fetching: rails-220.127.116.11.gem (100%) Successfully installed rails-18.104.22.168 Parsing documentation for thread_safe-0.3.5 Installing ri documentation for thread_safe-0.3.5 Parsing documentation for tzinfo-1.2.2 Installing ri documentation for tzinfo-1.2.2 Parsing documentation for minitest-5.8.4 Installing ri documentation for minitest-5.8.4 Parsing documentation for activesupport-22.214.171.124 unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping Installing ri documentation for activesupport-126.96.36.199 Parsing documentation for rack-1.6.4 Installing ri documentation for rack-1.6.4 Parsing documentation for rack-test-0.6.3 Installing ri documentation for rack-test-0.6.3 Parsing documentation for mini_portile2-2.0.0 Installing ri documentation for mini_portile2-2.0.0 Parsing documentation for nokogiri-188.8.131.52 unable to convert "\xCA" from ASCII-8BIT to UTF-8 for lib/nokogiri/nokogiri.bundle, skipping Installing ri documentation for nokogiri-184.108.40.206 Parsing documentation for loofah-2.0.3 Installing ri documentation for loofah-2.0.3 Parsing documentation for rails-html-sanitizer-1.0.3 Installing ri documentation for rails-html-sanitizer-1.0.3 Parsing documentation for rails-deprecated_sanitizer-1.0.3 Installing ri documentation for rails-deprecated_sanitizer-1.0.3 Parsing documentation for rails-dom-testing-1.0.7 Installing ri documentation for rails-dom-testing-1.0.7 Parsing documentation for builder-3.2.2 Installing ri documentation for builder-3.2.2 Parsing documentation for erubis-2.7.0 Installing ri documentation for erubis-2.7.0 Parsing documentation for actionview-220.127.116.11 Installing ri documentation for actionview-18.104.22.168 Parsing documentation for actionpack-22.214.171.124 Installing ri documentation for actionpack-126.96.36.199 Parsing documentation for activemodel-188.8.131.52 Installing ri documentation for activemodel-184.108.40.206 Parsing documentation for arel-6.0.3 Installing ri documentation for arel-6.0.3 Parsing documentation for activerecord-220.127.116.11 Installing ri documentation for activerecord-18.104.22.168 Parsing documentation for globalid-0.3.6 Installing ri documentation for globalid-0.3.6 Parsing documentation for activejob-22.214.171.124 Installing ri documentation for activejob-126.96.36.199 Parsing documentation for mime-types-2.99 Installing ri documentation for mime-types-2.99 Parsing documentation for mail-2.6.3 Installing ri documentation for mail-2.6.3 Parsing documentation for actionmailer-188.8.131.52 Installing ri documentation for actionmailer-184.108.40.206 Parsing documentation for thor-0.19.1 Installing ri documentation for thor-0.19.1 Parsing documentation for railties-220.127.116.11 Installing ri documentation for railties-18.104.22.168 Parsing documentation for bundler-1.11.2 Installing ri documentation for bundler-1.11.2 Parsing documentation for concurrent-ruby-1.0.0 Installing ri documentation for concurrent-ruby-1.0.0 Parsing documentation for sprockets-3.5.2 Installing ri documentation for sprockets-3.5.2 Parsing documentation for sprockets-rails-3.0.1 Installing ri documentation for sprockets-rails-3.0.1 Parsing documentation for rails-22.214.171.124 unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/akshaysurve.jpg, skipping unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/belongs_to.png, skipping unable to convert "\xF4" from ASCII-8BIT to UTF-8 for guides/assets/images/book_icon.gif, skipping unable to convert "\x91" from ASCII-8BIT to UTF-8 for guides/assets/images/bullet.gif, skipping ... ...continued on and on... ... unable to convert "\x80" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.gif, skipping unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.png, skipping unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/vijaydev.jpg, skipping Installing ri documentation for rails-126.96.36.199 31 gems installed
But, it worked:
$ rails --version Rails 188.8.131.52
Create Your Application and Framework Instance
Then I created a new application called blog:
You can browse the Rails web application framework directory structure within your application:
$ cd blog $ ls Gemfile Rakefile config lib test Gemfile.lock app config.ru log tmp README.rdoc bin db public vendor
Here's a view from the Getting Started guide showing how Rails relies on convention over configuration:
Launch Your Server
Finally, you can launch your server with a command line:
$ bin/rails server Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance. => Booting WEBrick => Rails 184.108.40.206 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2016-02-05 18:14:05] INFO WEBrick 1.3.1 [2016-02-05 18:14:05] INFO ruby 2.0.0 (2015-04-13) [universal.x86_64-darwin15] [2016-02-05 18:14:05] INFO WEBrick::HTTPServer#start: pid=30576 port=3000
Visit Your Application
In your browser, go to http://localhost:3000, and you should see your default home page for a generic Rails application:
I have to say, I wish all this was a bit cleaner. That said, there are some wonderful community resources to help you grow.
Here's an example of a free GoRails screencast covering the basic framework application architecture shown in the directory above:
And the Rails Guide itself walks you through a number of beginning steps:
Overall, there are a wide variety of written and screencast tutorials out there to guide you.
If you're intrigued by the quality of Ruby on Rails and its community, I hope you've found this introduction a helpful starting point. Maybe you've even tried the install above.
More recently, I'm intrigued by Apple's open sourcing of Swift, an exciting entrant into the object oriented programming language space, akin to Ruby with similar long-term potential to Rails.
While I've worked with Rails in the past, I've also had struggles with it, especially with hosting and expanding functionality — I've also found hosting it more expensive. I continue to be most comfortable with Yii, which you can read about in our Envato Tuts+ Yii2 Programming Series. And with PHP 7 emerging, the future looks faster and brighter for PHP-based programming frameworks.
If you have questions, please post them below. Or, you can contact me on Twitter @reifman. Please check out my Envato Tuts+ instructor page to see other tutorials I've written, such as Using Social Media to Locate Eyewitnesses, which highlights the APIs of Instagram and Twitter.