loading...
Cover image for A Guide to Using Ruby on Rails Application Templates

A Guide to Using Ruby on Rails Application Templates

justalever profile image Andy Leverenz Originally published at web-crunch.com ・4 min read

As my experience with Ruby on Rails continues to grow I explore more and more by hitting the ground running with a base application template for new projects. In this guide, I'll discuss how I created my most recent rails application template as well as what all get's included.

Ruby on Rails is by far the quickest path to a production worthy application that I know of. You can generate an entire blog in as little as 5 minutes using the built-in CLI. With that in mind, and my constant effort to provide tutorials around the framework, I took to building an application template for use going forward.

My Let's Build: With Ruby on Rails series has been a foundation for my learning of the framework. After several builds, I began to notice a lot of patterns involved in the way I needed to configure a new app each time I ran rails new.

Due to the nature of the tutorials, I wanted to provide "an approach from scratch" because many tutorials out there assume you know a lot before beginning. I still believe in this principle because practice makes perfect but in my particular case, I bypass a lot of the rudimentary configuration making use of an application template.

With an application template you can create a new Rails app and define preconfigured decisions based on what you find you use the most in your past/current projects. Assuming you need to use a template, this can be a huge time saver.

This might not be for you if you really don't build a large number of apps but could prove worthy if you have ideas that you eventually want to build and don't want to waste time configuring the app every time you kick something off.

What is a Ruby on Rails application template?

Application templates are simple Ruby files containing DSL for adding gems/initializers etc. to your freshly created Rails project or an existing Rails project.

In short, it's a way to install your favorite gems, configure your app, and even version control your project using one command.

How do I use the template?

If you're new to Ruby on Rails I invite you to check out my series so far linked at the bottom of this post. If you have some familiarity with the framework you may know by now that creating a new app is as simple as running rails new my_app.

Passing the template through works like this

$ rails new my_app -m template.rb

Here our CLI will look for a file called template.rb to guide it while it installs a new app.

You can download the latest template I created using Tailwind CSS out of the box. I called it kickoff_tailwind.

Download a copy of that and cd within the folder. There you can create your app by passing the template.rb file through as I stated before.

From there your app will scaffold and you can move the app folder wherever you like on your system.

What comes with it?

The included gems are:

  • devise
  • friendly_id
  • foreman
  • sidekiq
  • tailwindcss
  • webpacker

  • Webpack support + Tailwind CSS configured in the app/javascript directory.

  • Devise with a new username and name field already migrated in. Enhanced views using Tailwind CSS.

  • Support for Friendly IDs thanks to the handy friendly_id gem. Note that you'll still need to do some work inside your models for this to work. This template installs the gem and runs the associated generator.

  • Foreman support thanks to a Profile. Once you scaffold the template, run foreman start to initialize and head to locahost:5000 to get rails server, sidekiq and webpack-dev-server running all in one terminal instance. Note: Webpack will still compile down with just rails server if you don't want to use Foreman.

  • A custom scaffold view template when generating theme resources (Work in progress).

  • Git initialization out of the box

More Templates

Shameless plugs

If you liked this post, I have many more builds on YouTube and my blog. I plan to start authoring more here as well. Want more content like this in your inbox? Subscribe to my newsletter and get it automatically.




☝ Want to learn Ruby on Rails from the ground up? Check out my upcoming course called Hello Rails

Posted on Feb 14 '19 by:

justalever profile

Andy Leverenz

@justalever

Product Designer @memberful/@patreon, Blogger/Vlogger @webcrunchblog, Guitarist 🎸 and YouTuber. I enjoy whiskey, metal, and people watching. Most recent launch: Hello Rails (https://hellorails.io)

Discussion

markdown guide
 

Cool stuff, Andy. Recently, I've also been thinking about forking and customizing Suspenders gem to create my own Rails app template. Having a template is like taking Rails a step further in the sense that you can even take more advantage of the framework.

And it's also very repetitive every time a new project starts to configure and install gems and the like.

Yours and Jumpstart look really great though I'm wondering if Tailwind can be used alonside bootstrap?

Anyway, great stuff and thanks for sharing!

 

Hey Francisco!

I use templates in my own projects primarily for teaching. If you find yourself creating and configuring a lot of new apps often, these application templates are perfect.

You certainly could use Tailwind and Bootstrap together but I feel the weight of all that CSS might make the site/app a bit sluggish.