Sometimes we need to show tables of data in a Rails app, sort of like souped-up spreadsheets. The tables, or grids, should handle tons of records, support searching and filtering, and allow for customization.

Ever try coding a data grid yourself? It’s hard. You need to implement pagination, filtering, searching, ordering, etc., every time. Why not use a pre-built solution and built off of it instead?

Wice Grid is the quickest grid solution out there for Rails. It takes care of both the client and server. It’s not AJAX-y and gorgeous, but it’s functional and reliable.

At Cook Smarts, we used Wice Grid to improve the admin interface without leaping to a full-scale solution like Rails Admin.

For instance, Cook Smarts’ customers can purchase gift certificates for their friends. Site admins need to see all gift certificates and search for particular ones. The easiest way for them to do this is through a data grid.

Add the grid code to a controller method, with the model whose data should appear in the grid.

1

@gift_certificates_grid=initialize_grid(GiftCertificate)

Then, add the grid code to a view, referencing the grid you specified in the controller. Add columns, specifying the attribute as the field name in the database and the name as the text to appear in the header of the grid.

Most apps have an admin interface, a place where the developers and special users can make changes to the database.

Creating your own admin interface is time-consuming, and can take the focus away from developing user-facing features. Figuring that admin interfaces have similar needs, frameworks have emerged that help get your started.

With Rails Admin, you can edit your database on dev and production, in a user-friendly way, without coding your own admin interface.

Its closest competition is Active Admin, which I see as too complex for a convenience tool.

Quick start

Add this to your Gemfile:

1

2

gem'rails_admin'

In a terminal window, while inside the path of your app, run

1

2

railsgrails_admin:install

You now have an admin panel, located at http://yourapp.com/admin

Two essential steps remain.

One, make sure your models are defined correctly. Have you defined associations, like belongs_to, has_one, and has_many, as needed?

In closing

I love Markdown, and will use this tool whenever I need to maintain static, formatted content in my app.

Rolling out Markdown editing to end users is trickier, since many may not be familiar with it. However, allowing people to use Markdown is different from requiring it. If you allow Markdown in comments, for instance, place a note under your new comment field stating that Markdown is an option, and link to a quick reference.

Google Analytics provides solid, aggregate statistics for your Rails app. It answers questions like, “How many visitors do I receive a day?” and “Which parts of my app are most popular?”

It cannot track individual users over time, however. Questions like, “How long does the average user remain active over time?” or “How long does it take the average user to go from a free to a paid account” require user analytics.

Services like Mixpanel and KISSMetrics specialize in user analytics, offering robust tracking and reporting for a fee.

There is another option, a free, open-source Ruby gem (technically, a Rails engine) called Ahoy. In just a few minutes, you can have robust user analytics for your Rails app.

Let’s walk through an example; say that on our site we want to track visits and actions for each user.

First, install Ahoy and perform the post-installation steps. This takes only a few minutes.

Track visits

Visit tracking starts immediately after you install Ahoy. You can customize the length of each visit (i.e. after how long does one visit turn into two); it defaults to 4 hours.

Ahoy automatically ties each visit to a user. You not only know how many visits you receive each day, but which users visit the most. Already, we have user analytics.

Track events

Users do stuff in your app. If you have a todo list app, for instance, they add todo items. In modern-day apps, user actions may be processed on the server with Rails and/or on the client via JavaScript.

Reporting

Third-party services vs. Ahoy

Mixpanel and KISSMetrics offer advanced reporting out of the box, but have a cost and require you to send your user data to a third party. If you choose to leave either service, extracting your data in a useful way is difficult.

Ahoy is free and your data stays local, but you’ll spend time creating a stats dashboard and reports.

There is no right answer, but while evaluating user analytics tools, be sure to consider Ahoy alongside the paid options.

At Cook Smarts we rely on external services for payments, image processing, and more. Sometimes, the app will fail to reach a service. Perhaps the service is down or there’s a network error somewhere along the line.

How can we tolerate such errors with a minimum impact to the user?

Here’s an example of how we addressed a recurring error with an external service:

A user can export a recipe as a PDF, including an image of the meal.* We retrieve the appropriately-sized image form reSRC, our external image processor.

Occasionally, the connection to retrieve the image times out, preventing the image from importing into the PDF. Before, this caused an exception, preventing the user from exporting the recipe at all.

For a better user experience, we now show a friendly error message in place of the image.

To show the friendly error instead of a fatal exception, we use ‘begin’ and ‘rescue’ in Ruby, in the midst of our Prawn PDF code.