Getting Started with Fortitude

You can be up and running with Fortitude in only a few minutes.

We’ll start by showing you how to use Fortitude with Rails. If you’re not using Rails, you can see how to get started without Rails.

Fortitude is compatible with nearly any version of Ruby (1.8.7–2.2.x) and nearly any version of Rails (3.0.x–4.2.x) that you might be using. It is also fully supported (and accelerated) on JRuby as well as Ruby.

1. Add Fortitude to your Gemfile

Add the following line to Gemfile, in the root of your Rails application:

gem 'fortitude'

Now, run bundle install to install Fortitude.

2. Create a Base Widget Class

Fortitude expresses your views as simple Ruby classes. And, like all classes, they can inherit from each other.

Eventually, you’ll see how to use this as an incredibly powerful tool to build better-factored views. However, right now, we’re just going to make a single base class that all our views will inherit from. Among other things, this gives us an easy place to configure Fortitude.

Create a file named app/views/base.rb, and put this into it:

app/views/base.rb
class Views::Base < Fortitude::Widget
  doctype :html5
end

Right now, this does nothing but tell Fortitude which variant of HTML we’re using. (There is also :html4_transitional, :xhtml10_strict, and so on. But right now, we’ll assume you want to be modern and use HTML5.)

3. Create Your First View

We’re all ready to start using Fortitude. Let’s take a look at a sample controller and view using Fortitude.

First, our controller:

app/controllers/hello_controller.rb
class HelloController < ApplicationController
  def world
    @name = "Suzanne"
  end
end

Next, our view:

app/views/hello/world.html.rb
class Views::Hello::World < Views::Base
  needs :name

  def content
    h1 "Hello, #{name}"
  end
end