Ruby on Rails Fundamentals

A Dice Talent Community

Ruby on Rails locomotiveRuby on Rails is a full-stack, open-source Web framework used for writing real-world applications with more simplicity and less code than most XML frameworks. As the official RoR web site says, it’s “an open-source web framework that’s optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration.”

The happiness comes from Rails’s sustainable and productive approach to Web development. In 2005, when Rails was introduced, available frameworks were complicated to setup and developed mostly in Java and PHP. It left developers frustrated when it came to starting new projects—they had to begin from scratch in Java, setup a lot of XML files, and create several lines of code just to get a simple, “Hello World!” You had to know Struts, JSP, XML, Tomcat and more before you could get a Web project going. So based on that, one all-important thing was implemented on Rails and ported to several other frameworks: convention over configuration.

Rails app directoryThat’s a simple thing, unfortunately we, the developers, tend to make things complicated and create applications that you can configure in any number of ways. To streamline things, the core developers of Rails focused on convention and configuration only in the framework’s real guts. For example, all the controllers are called by a name and a single suffix: “_controller.” The names of database are referred to in plural (“products,” not “product” or “prod.”) It helps developers find what they’re looking for, quickly.

So, What Is Ruby?

Ruby was designed to focus on simplicity and productivity, and Rails takes advantage of these traits. In this environment, the concept of “DRY” —Don’t Repeat Yourself—is important, and Ruby provides a lot clever of ways to avoid code repetition. So, developers can do more with less. It needs more CPU power, but in this age of cheaper and cheaper hardware, that’s not much of an issue.

Behind the curtains, Ruby on Rails is a Model View Controller framework, with a pattern that’s among the most useful when developing Web applications. The framework’s main components are ActionPack, ActiveSupport, ActiveModel, ActiveRecord, ActiveResource, and ActionMailer. Essentially, Rails is a framework composed of other frameworks than can be used independently. Some details:

  • Action Pack: Handles requests and responses. This framework is part of Rails’s Model View Controller pattern, which serves the Web request, handling, routing, and view generation. To provide the response, it defines controllers that implement actions in order to render views.
  • Active Model: Provide the interfaces for the Model part of Model View Controller. This component is new in Rails 3.0. In previous versions, the model layer was based on ActiveRecord. In the current version, you can use any ruby class as a Model. This is very important because you can use your own persistence layer and glue it into Rails.
  • Active Record: This is the Object Relational Mapping (ORM) component of Rails, with a very nice zero-configuration feature. Naming and convention is the key to maintaining simple and minimal code to define classes that will be persisted in the database tables.
  • Active Support: a collection of utility classes and standard library of extensions that are useful in Rails. We can find this extensions useful for several Ruby projects.
  • Active Resource: Connects business objects and Representational State Transfer (REST) Web services. With ActiveResource, you can easily use REST to expose your ActiveRecord models with just a small amount of code. This is a useful way to create an API without much effort.
  • Action Mailer: This framework provides the email service layer, helping out to send the forgot password emails, registration emails, invoices for billing, etc. This class wraps ActionController from ActionPack to render the emails as page views, with the same render and templates like pages.

Rails control panel

Highlighted Features

  • ORM Embedded: no configuration needed.
  • Generators: for Controllers, Models, Views, Tests and other code that you’ll need. With just a command, you can have the whole CRUD (create, read, update, delete) working in your browser.
  • Migrations: With ActiveRecord, the DB structure is versioned. Migrations let you go backward and forward so the app can take care of any changes on the database structure.
  • Rack/Metal and Middleware: For speed, Rails provide access to the low level HTTP request/response handling, resulting in faster API access for example when rendering isn’t required. The Middleware uses the Rack interface to receive its request directly and sends the response directly to the front end. Middleware can also be used to inject fast handlers/pre-filters into requests.
  • REST and Polymorphic Controllers/Resources: ActionController provides a polymorphic way to respond to the URI based on the mime type. That means you can use the same method in the controller, and provide several responses, one in XML, one in PDF, another in JSON, and another in HTML. While you have to write the code anyway, you only have to render the associated view for each mime-type. The controller is simple and easy to understand in its handling of polymorphic requests pointing to the same resource.
  • Coffee Script: It helps developers who don’t want to deal with Javascript syntax and would rather create Javascript code in a more Ruby way. You can take a look here.
  • Rendering engines for All Tastes: On the View side, Rails provides a default rendering engine using ERB templating. However, you can use approaches like HAML or Slim, or create your own.
  • Rake and Command Line Tasks: When you have to do file maintenance for your application–moving files, running upgrade processes, etc.– Rails works with Rake so you can automate tasks in Ruby.
  • Dependencies Easier than Ever: Dependencies weren’t so well-handled in Rails initially, but then the new Bundler appeared (thanks Yehuda Katz (@wycats). It deals with them in a smart, easy-to-maintain way.
  • The Community: One of the keys to success with Rails is the Ruby community itself. It provides events for all flavors, from small to big, from local to global. I highly recommend getting involved and attending events to meet developers in person.

Can I use Rails in my next project?

Yes and no. It depends on what kind of project you’re undertaking, its requirements, and so on. But generally, I think it’ll fit for applications defined with commonplace requirements. Even if it doesn’t, there are certainly other Ruby frameworks you could use. If nothing else, ask the community. (Or me.)

Click here to return to the Ruby on Rails community.

THIS DICE TALENT COMMUNITY SPONSORED BY:

The Latest From Dice

Should Employers Ban After-Hours Work Email?

Posted In Living in Tech
Employee Asleep on Desk
Earlier this month, two French employee unions decided to show how they felt about work/life balance by signing a legally binding agreement that would forbid employees from checking work email after a long day at their desks. The agreement will affect only 250,000 employees in the technology and consulting sectors, according to The Guardian, although a number of them work for Google, Facebook, and PricewaterhouseCoopers (PwC)—not exactly firms known for allowing their workers to unplug from the grid. Click here… continue…

The Future of Robots: Soft, Flexible, Squeezable

Posted In Working in Tech
Otherlab Soft Robots
If someone asks you to picture a robot, the first image that probably comes to mind is the steel exoskeleton from “The Terminator” films, or one of those creepy mechanical “dogs” produced by Boston Robotics. But if teams of researchers at MIT’s Computer Science and Artificial Intelligence Laboratory (CSAIL) and other institutions succeed in their latest endeavors, the robots of the future could look very different: soft, squiggly, and flexible. Click here to find robotics jobs. “There are only maybe… continue…

Raleigh Employers Look for Software Developers

Posted In Looking in Tech
Raleigh
What’s New This Quarter It’s been a tumultuous time for Research Triangle-based employees of both Lenovo and IBM as the two companies work to forge a future that will be profitable for both. After a seventh straight quarterly sales decline, IBM expected to post about $1 billion in “workforce-rebalancing” costs during the first quarter. (A similar plan in 2013 led to some 3,300 layoffs in North America, including hundreds in North Carolina.) Lee Conrad, national coordinator for the employee organization… continue…

Why You Might Want to Hold Off Developing Apps for Windows 8

Windows 8
Microsoft executives have spent the past year and a half promoting Windows 8’s app store, in the hope that third-party developers would create a thriving ecosystem of games and productivity software. What’s the result of all that time and energy? As InfoWorld relates in a new posting, some 150,000 apps currently reside in the Windows Store—not a very impressive number, when you consider the number of Windows devices currently on the market, not to mention the hundreds of thousands of… continue…

Big Data Widens Opportunities for Business Analysts

Data Opportunities
One of the newest trends in business analysis is the melding of the BA and BI roles – where BAs become more like data-centric business intelligence analysts, similar to data scientists. As more companies jump onto the Big Data wagon, the widening need for data may breathe new life into the business analysis role, opening more avenues of responsibilities — and corresponding increases in salaries as a result. Click here to find business analyst jobs. Most BAs have technology and… continue…

Mark Zuckerberg’s Management Tips

Facebook Home App
Facebook CEO Mark Zuckerberg sat down with The New York Times recently to discuss some of his company’s latest moves, including its high-profile acquisition of WhatsApp. Much of that discussion focused on Creative Labs, which is tasked with building standalone apps—not all of which will feature Facebook’s branding. “In mobile there’s a big premium on creating single-purpose first-class experiences,” Zuckerberg told the Times’ Farhad Manjoo. Click here to find IT manager positions. On that front, Facebook’s Messenger app—which, as the… continue…

Silicon Valley Sees Skills Shortages in Java, .NET, PHP

Silicon Valley
What’s New This Quarter While blockbuster deals can seem all too common in Silicon Valley these days, Facebook’s January acquisition of text messaging service WhatsApp for a staggering $19 billion turned many of the region’s heads, with some experts contending that the company was actually worth even more. In late March, Facebook followed up the deal with the purchase of Kickstarter darling Oculus Rift for $2 billion, turning even more heads. Then there’s action camera maker GoPro, which is testing… continue…

How One Apple Employee Survived a Steve Jobs Product Review

Posted In Working in Tech
Steve Jobs
In the popular conception, the late Steve Jobs is a megalomaniacal jerk who screamed, bullied, and pushed until he got whatever he wanted. But according to Don Melton, who started the Safari and WebKit projects at Apple, Jobs was a bit misunderstood. “Steve was not some mercurial ogre or cartoon autocrat. He was just very, very busy,” Melton wrote in a recent posting on his personal site. “He didn’t have time for ‘yes men,’ the easily frightened, or those who… continue…

How to Get a Job at HomeAdvisor

Posted In Looking in Tech
Landing@ HomeAdvisor
The home-maintenance website HomeAdvisor offers comprehensive tools and resources that connect homeowners with a network of 85,000 pre-screened home improvement and repair service professionals. Headquartered in Golden, Colo., the company has over 1,200 employees globally, with 1,000 of them in Denver and Lenexa, Kan. Currently, it has some 100 technical and product team members, and open positions including junior-level Java developer, mid-level QA staff, senior-level platform automation professionals, and systems and network engineering roles. The company’s tech recruiters, Tanya Jones… continue…

In Seattle: Amazon Expands, Microsoft Adapts

Posted In Looking in Tech
Seattle Skyline
What’s New This Quarter Should Microsoft leave Seattle and head for Silicon Valley? That was the provocative question raised by Peter Cohan in a February Forbes column. His theory: Microsoft can no longer innovate, and its best hope is to immerse itself in the feverish environment of the Valley. “The lack of competing opportunity for Microsoft’s talent spurs complacency by employees and management,” he wrote. Of course, Seattleites immediately fired back. “In my opinion, being outside the echo chamber of… continue…