Quantcast
Channel: Lasse Bunk's weblog » Programming
Viewing all articles
Browse latest Browse all 5

What is Ruby on Rails? (an introduction)

$
0
0

So what is Ruby on Rails? Here’s an introduction.

Ruby (the programming language)

From Wikipedia: “Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro “Matz” Matsumoto. It was influenced primarily by Perl, Smalltalk, Eiffel, and Lisp.” Ruby is a beautiful language – you’ll love it. Read more on ruby-lang.org and try it here.

Rails (the framework)

Rails is a web framework built on Ruby, hence the name Ruby on Rails. It enables the programmer to easily create advanced websites, following convention over configuration, which means that if you stick to a certain set of conventions, a lot of features will work right out of the box with very little code.

MVC (Model-View-Controller)

Rails is based on a programming pattern called MVC, which stands for Model-View-Controller. Here I’ll try to explain what it is and why it makes sense to use it.

Model

The model in MVC is a class for each entity in your application. Example models are User, Category, or Product. The models hold all business logic, for example what should happen if you delete a product, add a new category, or create a new user.

View

The views in MVC hold all your presentation and presentation logic, normally HTML. It is based on ERuby which is a templating system that allows for Ruby embedded into HTML and other languages. You probably know this kind of templating system from ASP, PHP, or JSP.

Controller

The controller is what binds together the models and views, for example tells the show product view which product it should show, or handles the data from a form POST when a user updates a shopping basket.

Why does MVC make sense?

MVC allows for perfect separation of business logic from presentation logic, giving you a much cleaner application that is easier to maintain and, on top of that, a lot more fun to develop.

Routing

So, how does is a request from a visitors browser sent down to the controller that handles this request? This is done by a routing engine that interprets the request and passes it on to the matching controller.

CRUD (Create, Read, Update, Delete)

In the heart of the Rails routing engine lies CRUD, or Create, Read, Update, Delete, which comes from a notion that all web pages are made up of these four actions. For example, you create a product, view, or read a user, edit, or update a category, and delete an order.

If you follow this pattern, you’ll get a lot of work done for you. For example, all it takes to create all of these actions for a product is to tell the routing engine that you have an entity called product, and it will wire up all the routing logic that binds the request to the matching controller for you.

ActiveRecord

ActiveRecord is a pattern that allows for all database access to be written without a single line of SQL or other database language. You tell the class, or model, what kind of entity you want it to be, which relations it has, and it automagically does the rest for you, creating properties based on the fields from the database, giving you database operation methods like create, update, and delete for free. You write only your custom business logic. The ActiveRecord classes are almost always what makes up the Model part of the aforementioned MVC pattern.

Database migrations

In Ruby on Rails you almost never talk directly to your database like you know it from PHP or other scripting languages. Instead, as part of ActiveRecord, comes a complete database migration framework. Database migrations mean that you create Ruby code for creating tables and adding columns instead of doing this directly in your database. What this does it that it allows for migrating multiple databases with the same changes, for example a development and production database, without having to remember what changes you made in your development database when deploying to production – you just run the same migrations in a different environment.

Separation of environments (development, production, etc.)

In Rails, you have a perfect separation of environments. This means that you can run your system with different settings based on which environment you are currently in, for example you want your development environment to display verbose error messages where your production site shows a user friendly message to the end user. Development, production, and test databases are also separated.

Assets management (images, stylesheets, and javascripts)

The Rails 3.1 assets pipeline allows for all your assets (images, stylesheets, and javascripts) to be managed for you. In your development environment you have your full folder structure with stylesheets and javascripts written in different languages like CoffeeScript and SCSS. When you deploy, all your assets are compiled and fetched from the same folder, all stylesheets compiled together, the same with your javascripts. This also means that you don’t have to manually include for example jQuery – this is included for you via the jQuery Rails plugin.

Plugins (RubyGems etc.)

Coding in Ruby on Rails means that you get a butt load of plugins already written for you. Not in the Rails core (it’s very clean) but as RubyGems which the Ruby package manager. For example, you could have plugins for pagination, search engine optimization, or image processing. There are many thousand RubyGems to be used for free.

Conclusion

Ruby on Rails is a great framework for creating advanced web applications writing very little code compared to what you get. I recommend it for almost any kind of application, and for prototyping new web applications. To inspire you, I created a video where I create a blog in 10 minutes using Ruby on Rails. Check it out and be sold. I also recommend the documentation section of the official Ruby on Rails site. And check out my examples of Ruby on Rails.


Viewing all articles
Browse latest Browse all 5

Trending Articles