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-4.2.5.1.gem (100%) Successfully installed activesupport-4.2.5.1 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-1.6.7.2.gem (100%) Building native extensions. This could take a while... Successfully installed nokogiri-1.6.7.2 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-4.2.5.1.gem (100%) Successfully installed actionview-4.2.5.1 Fetching: actionpack-4.2.5.1.gem (100%) Successfully installed actionpack-4.2.5.1 Fetching: activemodel-4.2.5.1.gem (100%) Successfully installed activemodel-4.2.5.1 Fetching: arel-6.0.3.gem (100%) Successfully installed arel-6.0.3 Fetching: activerecord-4.2.5.1.gem (100%) Successfully installed activerecord-4.2.5.1 Fetching: globalid-0.3.6.gem (100%) Successfully installed globalid-0.3.6 Fetching: activejob-4.2.5.1.gem (100%) Successfully installed activejob-4.2.5.1 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-4.2.5.1.gem (100%) Successfully installed actionmailer-4.2.5.1 Fetching: thor-0.19.1.gem (100%) Successfully installed thor-0.19.1 Fetching: railties-4.2.5.1.gem (100%) Successfully installed railties-4.2.5.1 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-4.2.5.1.gem (100%) Successfully installed rails-4.2.5.1 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-4.2.5.1 unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping Installing ri documentation for activesupport-4.2.5.1 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-1.6.7.2 unable to convert "\xCA" from ASCII-8BIT to UTF-8 for lib/nokogiri/nokogiri.bundle, skipping Installing ri documentation for nokogiri-1.6.7.2 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-4.2.5.1 Installing ri documentation for actionview-4.2.5.1 Parsing documentation for actionpack-4.2.5.1 Installing ri documentation for actionpack-4.2.5.1 Parsing documentation for activemodel-4.2.5.1 Installing ri documentation for activemodel-4.2.5.1 Parsing documentation for arel-6.0.3 Installing ri documentation for arel-6.0.3 Parsing documentation for activerecord-4.2.5.1 Installing ri documentation for activerecord-4.2.5.1 Parsing documentation for globalid-0.3.6 Installing ri documentation for globalid-0.3.6 Parsing documentation for activejob-4.2.5.1 Installing ri documentation for activejob-4.2.5.1 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-4.2.5.1 Installing ri documentation for actionmailer-4.2.5.1 Parsing documentation for thor-0.19.1 Installing ri documentation for thor-0.19.1 Parsing documentation for railties-4.2.5.1 Installing ri documentation for railties-4.2.5.1 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-4.2.5.1 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-4.2.5.1 31 gems installed
But, it worked:
$ rails --version Rails 4.2.5.1
Create Your Application and Framework Instance
Then I created a new application called blog:
$ rails new blog create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/assets/javascripts/application.js create app/assets/stylesheets/application.css create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/views/layouts/application.html.erb create app/assets/images/.keep create app/mailers/.keep create app/models/.keep create app/controllers/concerns/.keep create app/models/concerns/.keep create bin create bin/bundle create bin/rails create bin/rake create bin/setup create config create config/routes.rb create config/application.rb create config/environment.rb create config/secrets.yml create config/environments create config/environments/development.rb create config/environments/production.rb create config/environments/test.rb create config/initializers create config/initializers/assets.rb create config/initializers/backtrace_silencers.rb create config/initializers/cookies_serializer.rb create config/initializers/filter_parameter_logging.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/session_store.rb create config/initializers/wrap_parameters.rb create config/locales create config/locales/en.yml create config/boot.rb create config/database.yml create db create db/seeds.rb create lib create lib/tasks create lib/tasks/.keep create lib/assets create lib/assets/.keep create log create log/.keep create public create public/404.html create public/422.html create public/500.html create public/favicon.ico create public/robots.txt create test/fixtures create test/fixtures/.keep create test/controllers create test/controllers/.keep create test/mailers create test/mailers/.keep create test/models create test/models/.keep create test/helpers create test/helpers/.keep create test/integration create test/integration/.keep create test/test_helper.rb create tmp/cache create tmp/cache/assets create vendor/assets/javascripts create vendor/assets/javascripts/.keep create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep run bundle install Your user account isn't allowed to install to the system Rubygems. You can cancel this installation and run: bundle install --path vendor/bundle to install the gems into ./vendor/bundle/, or you can enter your password and install the bundled gems to Rubygems using sudo. Password: Fetching gem metadata from https://rubygems.org/........... Fetching version metadata from https://rubygems.org/... Fetching dependency metadata from https://rubygems.org/.. Resolving dependencies.... Rubygems 2.0.14 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation. Installing rake 10.5.0 Using i18n 0.7.0 Installing json 1.8.3 with native extensions Using minitest 5.8.4 Using thread_safe 0.3.5 Using builder 3.2.2 Using erubis 2.7.0 Using mini_portile2 2.0.0 Using rack 1.6.4 Using mime-types 2.99 Using arel 6.0.3 Installing debug_inspector 0.0.2 with native extensions Using bundler 1.11.2 Installing byebug 8.2.2 with native extensions Installing coffee-script-source 1.10.0 Installing execjs 2.6.0 Using thor 0.19.1 Using concurrent-ruby 1.0.0 Installing multi_json 1.11.2 Installing sass 3.4.21 Installing tilt 2.0.2 Installing spring 1.6.3 Installing sqlite3 1.3.11 with native extensions Installing rdoc 4.2.1 Using tzinfo 1.2.2 Using nokogiri 1.6.7.2 Using rack-test 0.6.3 Using mail 2.6.3 Installing binding_of_caller 0.7.2 with native extensions Installing coffee-script 2.4.1 Installing uglifier 2.7.2 Using sprockets 3.5.2 Installing sdoc 0.4.1 Using activesupport 4.2.5.1 Using loofah 2.0.3 Using rails-deprecated_sanitizer 1.0.3 Using globalid 0.3.6 Using activemodel 4.2.5.1 Installing jbuilder 2.4.0 Using rails-html-sanitizer 1.0.3 Using rails-dom-testing 1.0.7 Using activejob 4.2.5.1 Using activerecord 4.2.5.1 Using actionview 4.2.5.1 Using actionpack 4.2.5.1 Using actionmailer 4.2.5.1 Using railties 4.2.5.1 Using sprockets-rails 3.0.1 Installing coffee-rails 4.1.1 Installing jquery-rails 4.1.0 Using rails 4.2.5.1 Installing sass-rails 5.0.4 Installing web-console 2.3.0 Installing turbolinks 2.5.3 Bundle complete! 12 Gemfile dependencies, 54 gems now installed. Use `bundle show [gemname]` to see where a bundled gem is installed. Post-install message from rdoc: Depending on your version of ruby, you may need to install ruby rdoc/ri data: <= 1.8.6 : unsupported = 1.8.7 : gem install rdoc-data; rdoc-data --install = 1.9.1 : gem install rdoc-data; rdoc-data --install >= 1.9.2 : nothing to do! Yay! run bundle exec spring binstub --all 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. * bin/rake: spring inserted * bin/rails: spring inserted
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 4.2.5.1 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.
Going Deeper
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.
In Closing
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.
Comments