Ruby on Rails
From StudentDevWiki
Ruby on Rails, often shortened to RoR or just Rails, is a web application framework that uses the Ruby language. The framework allows for rapid development, allowing you to quickly and easily create database-driven web applications. It implements a MVC (Model-View Controller) architecture for organizing code and development tasks, and features an Active Record system that allows to applications to interact with the database in a natural, object-oriented fashion.
If you're starting to think we at the Web Technologies group are a little enamored with this framework, you've vastly underestimated our relationship with it. Ruby on Rails drives the Student Senate website, MyRPI, and nearly all of the related services and applications. Be forewarned, however, that Ruby is a young language, and the Rails Framework is even younger. While it is quickly gaining in popularity, the Rails community doesn't measure up to the followings of, say, PHP and other popular languages. There are questions about its scalability and documentation is somewhat limited. Nonetheless, we'll try to provide you with the resources you'll need to get started with this powerful tool.
For further general information on Ruby on Rails, the Wikipedia Article on the topic is strongly recommended. Another great resource is rubyonrails.org.
Contents |
Ruby on Rails on the Student Development Server
Implementation Details and Notes
First, note that myRPI Rails Apps are created and managed through cPanel. This means that you never want to attempt to create, start, or stop a rails application from the command line. Gems are also managed through cPanel. However generators, debuggers, and all the usual interfaces to Rails are available via the shell.
Getting Started: A brief tutorial
The process for starting a Rails application is fairly straightforward. In cPanel, click on Ruby on Rails under the Software / Services section. Chose a short name for your application, and either accept the default path for a directory within your home folder, or place it somewhere else. If you are creating a new application, you likely want to select the Development Environment to enable debugging messages and avoid caching. You can always change the environment later through the table at the bottom of this page.
When you click 'Create' the application will now show up in the 'Available Ruby on Rails Applications' table. To power up the application, click the 'Run' button. You may also want to check 'Load on Boot' to make sure the application does not get turned off during system maintenance. Verify that your app is working by clicking the link in the URL column. A page such as http://yourdomain.myrpi.org:12345 will open, showing the default Rails Welcome page. If this works, congratulations! You have started your Rails app.
Clicking on 'About your application's environment' will reveal an error - 'no such file to load -- sqlite3'. This is because your database is not yet configured. You have several options for configuring your database. I will go into mySQL, because it is a robust database that is supported by myRPI, and because I'm very familiar with it. Configurations with PostgreSQL, sqlite3, and external databases should also be possible.
SQLite3 is a very lightweight and feature-limited sql database. We don't have a good browser for it up and running, and it's not as robust or responsive as using mySQL or Postgres, but it is easy to configure. You'll need to install a particular version of the gem, though (at least for now) - go to the command line and run 'gem install sqlite3-ruby --version 1.2.3'. Because this is a change to your app's environment, you'll need to go to the Rails page on CPanel, and stop and then re-start your application. Your database will be created in db/development.db (assuming you're in the development environment).
Rails Concepts
The following is heavily borrowed from the Rails Wikibook, and therefore falls under the GNU Free Documentation Licencse
The MVC Pattern
This section provides an explanation of the Model-View-Controller pattern, which is the basis for the Rails framework.
Model
The model represents the data and business logic for your application. In Rails, models are implemented as ActiveRecord classes.
Specifically, a model is a class that inherits from ActiveRecord::Base. Its name is singular and in CamelCase, whereas the name of the table to which it corresponds is conventionally plural and in snake_case.
View
The view is the presentation layer for your application. The view layer is responsible for rendering your models into one or more formats, such as XHTML, XML, or even Javascript. Rails supports arbitrary text rendering, and thus all text formats, but also includes explicit support for Javascript and XML.
In Rails, views are implemented using ERb by default.
Controller
The controller connects the model with the view.
In Rails, controllers are implemented as ActionController classes.
MVC Best Practices
Current best practices include:
- fat model and skinny controller: List conditions and data relationships can usually be specified in the model.
- business logic should always be in the model
- the view should have minimal code: the data to be displayed should have been prepared in the controller.
Convention over Configuration
Convention over configuration refers to reducing the number of configuration files you have and instead bowing to convention as the means of locating resources and handling other configuration items which in the past have been handled in config files.
Rails is an opinionated framework. If you want everything to work smoothly then it is best to conform to specific naming conventions:
- Source code files are always lowercase_and_underscored.rb
- Class names are always camelCase
- Models are singular, table names are plural (class Person = table people)
- Controllers are up for grabs, but the convention is plural (especially with the introduction of REST)
- Primary keys are called id
- Foreign keys are called name_id, so for example, if a Person has many Pets then the pet table would include a person_id column. More information on associations in Rails can be found in the ActiveRecord section of the wikibook.
RoR Resources
- rubyonrails.org - Official Website
- railscasts.com archive - Great narrated tutorials on accomplishing a variety of tasks in Rails
