The Ups and Downs of Rails Magic
By: Umar Ghouse
Tags: Ruby on Rails, JavaScript, Web Development, Code, Framework

I love using Ruby on Rails to code websites — being a well-established web framework, it makes the process quick and easy. Not to mention I find it a joy to code in Ruby! But we recently moved to a completely JavaScript stack at the office (Node.js on the backend and React on the front-end) and that’s opened my eyes to the ups and the downs of working in a Rails-based environment.

TL;DR

Rails works well if you need to bring something from idea to finished product fast. Use something like JavaScript, if you want more clarity and control.

What the heck is Rails Magic?

Coding is a science!

I hear you and to that I say, “It can sometimes feel like magic … you muggle 😏” — I mean, just look at AI! Rails magic is what I’d describe as the things that Rails does for you. It eliminates “boilerplate”/standard code and allows you to focus on the stuff that sets your product apart from everything else.

Running a web-app is complex — you need to set up a server environment, make sure your controllers, models and other code are wired up properly etc. Rails can feel like magic, because you don’t actually do any of that stuff and it just works, because Rails handles all that for you.

How does that work?

Rails follows an ideology of “convention over configuration”. This means that in the Rails world, code files must be arranged in certain, predictable folder structures or code must be written in certain ways — i.e. there is an established convention for how Rails wants you to write that code. This makes it easier for certain features or aspects of building a web-app to be automated, since it’s clear where the code must be added/changed and what must be done.

Sounds good so far, tell me more!

Rails can help you get going on a web project much faster than most other frameworks out there. In fact, if you don’t really care about styling, you can have something like a functioning blog (something that can put posts on the internet) up and running in about 15 minutes. How does it do that? Well, you just type in the following few lines of code into your Terminal and voila, you have a blog:

# Generate the blog project, in this example, we call it “blog” and set the database to postgres

rails new blog -d postgresql # Add the ability to create, read, update and delete posts, where each post has a title and a body

rails g scaffold Post title:string body:text # Make sure your posts are saved to the database

rails db:migrate

You now have a functioning (if ugly and somewhat insecure) blog. It’s ugly because it uses Rails’ default styling, which uses a lot of tables and doesn’t really care about looks.

Quick note here, when I say insecure, I just mean you don’t really have user accounts, meaning that anyone can post on your blog etc. I do not mean that it’s easily hackable — Rails takes security quite seriously and builds in many features that help establish better security out-of-the-box.

In addition to doing all this stuff for you, Rails also hides a lot of the boilerplate code, so your code base looks much simpler and more focused on what you want it to do. Here’s an example:

Let’s say you have a posts controller. The code to get the details of a single post in a node.js application (built using the express.js package) would look like this:

async getOne(req, res, next) {

const post = await Post.findByPk(req.params.id)

if (!post) return res.status(404).send('Post not found')

return res.status(200).send(post)

}

In Rails, it would look like this:

def show

end

Rails has already handled all the code related to grabbing the required post and leaves us to simply decide what to do once we have it — which in most cases, is nothing, so it just sends that data to the front-end!

Now, let’s talk about user accounts — what if you want to add those, is that easy too?

Enter, gems 💎!

Almost like infinity stones 😬

Yes, it is! I’ve generally used a Rails gem (that’s what the Ruby programming language calls plugins/packages) called Devise to get my user accounts and authentication set up.

Being a web framework that is well used (websites as big as GitHub, Fiverr and Shopify are built on Rails), there are a ton of useful gems that each help add functionality that the typical website may need.

Do you need your forms to accept data for nested models? Use Cocoon. Need to easily handle environment variables between your development and production systems? Use Figaro. Need an admin dashboard? Use administrate. What about pagination? Use Kaminari. You get the point.

A lot of these gems are (almost) plug and play, because, like Rails, they handle a lot of the complex stuff for you and expose a few simple APIs that you can use to add their functionality to your app. That doesn’t mean they aren’t customizable — many of these gems are built to allow you to use them the way that your app needs them to be used.

That’s cool, but what if you want more automation!

Rails can help there too. Rails introduced a concept called “templates” a while back, which essentially allows someone to package together certain gems and write some code to handle the set up of those gems, so that you can not only generate a basic rails app when starting a new project, but you can add all sorts of functionality (like user accounts or bootstrap) right out of the gate.

One template I’ve been very interested in has been the Jumpstart template. It essentially allows you to add user authentication, announcements, an admin dashboard and more with just 1 line of code in your terminal:

rails new myapp -d postgresql -m https://raw.githubusercontent.com/excid3/jumpstart/master/template.rb

What if you don’t need an entire app, but just need to set up a small part of it? Check out RailsBytes (co-created by the developer for the Jumpstart template above) — these put together gems to do little tasks, like a quick template to add and set up Bootstrap with Webpack in Rails 6, for example. Once again, just running something like the following in your terminal is all you need to get going:

rails app:template LOCATION='https://railsbytes.com/script/x9Qsqx

Pretty cool, right?