But What Does It All Mean?
“Ruby on Rails” is catchy but confusing. Is Rails some type of magical drug that Ruby is on? (Depending on who you ask, yes.)
Ruby is a programming language, similar to Python
and Perl. It is dynamically typed (no need for “int i”), interpreted,
and can be modified at runtime (such as adding new methods to classes).
It has dozens of shortcuts that make it very clean; methods are rarely
over 10 lines. It has good RegEx support and works well for shell
scripting.
Rails is a gem, or a Ruby library. Some gems let you use the Win32 API. Others handle networking. Rails helps make web applications, providing classes for saving to the database, handling URLs and displaying html (along with a webserver, maintenance tasks, a debugging console and much more).
IRB is the interactive Ruby console (type “irb” to use). Rails has a special IRB console to access your web app as it is running (excellent for live debugging).
Rake is Ruby’s version of Make. Define and run
maintenance tasks like setting up databases, reloading data, backing up,
or even deploying an app to your website.
Erb is embedded Ruby, which is like PHP. It lets you mix Ruby with HTML (for example):
Hello there, <%= get_user_name() %>
YAML (or YML) means “YAML Ain’t a Markup Language” — it’s a simple way to specify data:
{name: John Smith, age: 33}
It’s like JSON, much leaner than XML, and used by Rails for setting configuration options (like setting the database name and password).
Phew! Once Ruby is installed and in your path, you can add the rails gem using:
gem install rails
In general, use gem install “gem_name”, which searches online sources
for that library. Although Rails is “just another gem”, it is the
killer library that brought Ruby into the limelight.
Understanding The Model-View-Controller Pattern
Rails is built around the model-view-controller
pattern. It’s a simple concept: separate the data, logic, and display
layers of your program. This lets you split functionality cleanly, just
like having separate HTML, CSS and Javascript files prevents your code from mushing together. Here’s the MVC breakdown:
- Models are classes that talk to the databse. You find, create and save models, so you don’t (usually) have to write SQL. Rails has a class to handle the magic of saving to a database when a model is updated.
- Controllers take user input (like a URL) and decide what to do (show a page, order an item, post a comment). They may initially have business logic, like finding the right models or changing data. As your rails ninjitsu improves, constantly refactor and move business logic into the model (fat model, skinny controller). Ideally, controllers just take inputs, call model methods, and pass outputs to the view (including error messages).
- Views display the output, usually HTML. They use ERB and this part of Rails is like PHP - you use HTML templates with some Ruby variables thrown in. Rails also makes it easy to create views as XML (for web services/RSS feeds) or JSON (for AJAX calls).
The MVC pattern is key to building a readable, maintainable and easily-updateable web app.
Understanding Rails’ Directory Structure
When you create your first rails app, the directories are laid out
for you. The structure is well-organized: Models are in app/models,
controllers in app/controllers, and views in app/my_local_views (just
kidding).
The naming conventions are important – it lets rails applications
“find their parts” easily, without additional configuration. Also, it’s
very easy for another programmer to understand and learn from any rails
app. I can take a look at Typo, the rails blogging software, and have a
good idea of how it works in minutes. Consistency creates comprehension.