Skip to content

Latest commit

 

History

History
68 lines (51 loc) · 1.34 KB

getting_started.md

File metadata and controls

68 lines (51 loc) · 1.34 KB

Rails setup guide

BABL comes with its own ActionView's template handler supporting *.babl files.

Installation

Add this line to your Gemfile:

gem 'babl-json'

In order to activate Rails integration, BABL needs to be required after Rails. For instance, you can do:

require 'rails'
require 'babl'

Alternatively, if BABL cannot be required before Rails for some reasons, you can call this later, when you know that Rails is loaded:

require 'babl/railtie'

Configuration (optional)

Create a new initializer initializers/babl.rb:

Babl.configure do |config|
    # This path is used to find partials
    # (required only if you want to use partials)
    config.search_path = Rails.root.join('app', 'views')

    # Enable or disable pretty formatting of JSON outputs
    config.pretty = Rails.env.development?
end

Hello world

Create a new template app/views/my_resources/hello_world.babl containing:

object(
    answer_to_life: _
)

Create a new action/route in a controller:

class MyResourcesController < ActionController::Base
    def hello_world
        render 'hello_world', locals: { answer_to_life: 42 }
    end
end

Call this route, you should get:

{
    "answer_to_life": 42
}

Congratulations, BABL is ready to use !


Read BABL concepts