Ruby on Rails, also known as Rails, is a popular framework suited to nearly any web application. Many well-known commercial sites, such as Airbnb, GitHub, Bloomberg, and Basecamp—the site it was created for—use it today.
Rails is known for its ease of use and scalability. You can get a site with a complete database back end up and running in minutes. The framework also makes deploying sites from development to production environments effortless.
This post will cover how to install Rails, create a new site, and troubleshoot basic site problems. Then we’ll walk through how to integrate Rails into SolarWinds® Papertrail™.
What Is Ruby on Rails?
Ruby on Rails gets its name from Ruby, an interpreted object-oriented scripting language. It’s a server-side web application framework. Rails provides an active record interface to databases, offers a model-viewer-controller (MVC) framework for web applications, and makes it easy to use web standards like JSON for data transfer between client and server.
Rails makes it easy to build web applications quickly. Many common tasks and patterns are already implemented for you, so you don’t spend time writing “boilerplate” code. Don’t Repeat Yourself (DRY) and Convention over Configuration (CoC) are two important principles Ruby on Rails embodies in its design and implementation.
Rails is an opinionated framework. If you agree with those opinions, working with it is a pleasure. Development goes fast, and things usually work the way you expect. If you don’t go along with those opinions, Rails is not for you, and you’ll feel constrained.
While the design of Ruby on Rails makes it easy to build and maintain applications, its performance often pales compared to competitors like Django and NodeJS.
Getting Started With Ruby on Rails
One of the strongest advantages of Rails is the abundance of good documentation. It’s easy to get up and running quickly. There’s an excellent getting started guide here. Let’s go through the steps to get a website running. While we proceed, we’ll add a few hints and some background for each step.
To run Ruby on Rails, you need to install several dependencies.
First, you need to install Ruby.
Rails requires version 2.5.0 or newer. The current version is 3.0.3. MacOS comes with Ruby already installed, and most Linux distributions will have a recent version available via their package management systems. For Windows, you’ll need to download the installer and run it.
If you’re using your Linux distribution’s package manager, you need to install the Ruby development package, too. The getting started guide doesn’t mention this, but it’s important. For Ubuntu, you need to run apt install ruby-dev. For Redhat, use yum install ruby-devel.
Next, Rails won’t run without a database for storing its active records. The getting started guide uses SQLite3, a file-based database management system. Most Linux distributions come with it already installed.
If it’s not already there, download the correct version for your system and install it.
SQLite has a development package, too, and if you don’t install it, creating a new website will fail. For Ubuntu, the package is named libsqlite3-dev; for Redhat, libsqlite3-devel.
First, install Node.js from the package for your platform. This installs node and npm to your computer.
Now you can use npm to install yarn.
With the prerequisites installed, you can install Rails and create your new website.
Install Ruby on Rails
Start by installing the Rails gem with gem install rails. Depending on your system, you may need to run this command with sudo.
When it’s finished, create a new directory and switch to it.
Now, create your first website with rails new <website name>.
Rails will work for a few minutes before declaring success.
When it’s finished, change to the new directory created for your site and do a listing. You’ll see Rails created several new directories. The Rails documentation has a breakdown for each folder here.
Run the development server to see your new site in action with bin/rails server.
Go to http://127.0.0.1:3000 to see the default home page.
Adding New Content to Ruby on Rails
From here, you can use rails to add a new controller, which is how you add pages to your website. The guide has you edit the routes file before creating a new one, but you don’t have to.
Let’s try the easy way.
Run /bin/rails generate controller About index
Generate created a directory named app/views/about and put a file called index.html.erb in it. That’s the “view” in Model Viewer Controller. It also created another file named about_controller.rb and a corresponding test file. That’s the Controller.
Start your site again, and then point your web browser at http://127.0.0.1:3000/about/index.
Here’s what you’ll see in the browser.
The page is pointing you to where it’s located in the site’s directory structure.
Before we move on to some troubleshooting tips, let’s edit the new page.
Leave the server running and open app/views/about/index.html.erb in a text editor.
Change the text inside the paragraph <p> tags.
Now, refresh the page in your browser.
Troubleshooting Ruby on Rails
Now that you’ve seen how easy it is to get Rails up and running, let’s look at a few troubleshooting procedures.
Configure Rails Logging
First, you need to configure your Rails logs before you have any problems to troubleshoot.
Let’s add a timestamp to your site logs. Edit config/environment.rb in your site’s directory and add these three lines:
Rails.logger = Logger.new(STDOUT)
Rails.logger.level = Logger::DEBUG
Rails.logger.datetime_format = “%Y-%m-%d %H:%M:%S”
Start your server and request a page. You’ll see timestamps now.
Let’s introduce a bug, see what it looks like to a user, and find the issue using logs.
Edit apps/controllers/about_controller.rb to look like this:
class AboutController < ApplicationController
raise "Not ready!"
When a client requests an about page, this code throws an exception.
Run your server, but with a new command line option:
$ bin/rails server -e production
This puts your server in production mode. It will log less and present users with different error pages.
Open your about page at http://127.0.0.1:3000/about/index.
This is the error page Rails uses when your server has an internal issue. Now, let’s look at the logs.
These logs show the page requested at 21:10:14 and then the exception thrown at the same time.
Watching your logs for RuntimeError will help you isolate these problems before they become serious enough to drive users away.
Page Not Found
Let’s look at another common Rails problem. We’ll request a page that doesn’t exist.
Request about/indx instead of about/index.
Rails displays an error page telling the user the page doesn’t exist:
It also logs a routing error:
Rails is using routing to find pages. You can read a simple introduction to routes in the getting started guide.
So, scanning logs for RoutingError is an excellent way to discover common typos and issues created by moving content.
Using Papertrail With Rails
Rails logs are helpful for isolating issues, avoiding recurring problems, and providing better service to your clients. Papertrail can help you take your log management and searching to the next level, and configuring Papertrail with Rails is very straightforward and takes just a few minutes.
Papertrail uses remote_syslog2 to stream your Rails logs to Papertrail. To get started, add the remote_syslog_logger to your Gemfile. If you’re not using a Gemfile, then use the $ gem install remote_syslog_logger to install the gemfile and set the configuration file to log via remote_syslog_logger.
The last step is to update the host and port information to match your Papertrail log destination settings.
config.logger = RemoteSyslogLogger.new('logsN.papertrailapp.com', XXXXX)
It’s that simple! Your logs will now flow into Papertrail and you can start tailing and searching logs.
Get Started With Ruby on Rails
In this post, you learned what Ruby on Rails is, how easy it is to get started with it, and how to use Rails logs to troubleshoot the most common issues. Then, we wrapped up by looking at how to send your production logs to Papertrail for central access and indexing.
Get started with Rails and SolarWinds Papertrail today.
This post was written by Eric Goebelbecker. Eric has worked in the financial markets in New York City for 25 years, developing infrastructure for market data and financial information exchange (FIX) protocol networks. He loves to talk about what makes teams effective (or not so effective!).