by joshsusser

joshsusser / informal

Easily use any Plain Old Ruby Object as the model for Rails form helpers.

124 Stars 15 Forks Last release: Not found MIT License 13 Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


Informal is a small gem that enhances a Plain Old Ruby Object so it can be used with Rails 3 form helpers in place of an ActiveRecord model. It works with the Rails

helper, and
as well.

Here's a quick (and slightly insane) example:

# models/command.rb
require "informal"
class Command
  include Informal::Model
  attr_accessor :command, :args
  validates_presence_of :command
  def run; `#{command} #{args}`; end



def create command =[:command]) if command.valid? end end


It's a Ruby gem, so just install it with

gem install informal
, add it to your bundler Gemfile, or do whatever you like to do with gems. There is nothing to configure.


The insanity of the above example aside, Informal is pretty useful for creating simple RESTful resources that don't map directly to ActiveRecord models. It evolved from handling login credentials to creating model objects that were stored in a serialized attribute of a parent resource.

In many ways using an informal model is just like using an AR model in controllers and views. The biggest difference is that you don't

an informal object, but you can add validations and check if it's
. If there are any validation errors, the object will have all the usual error decorations so that error messages will display properly in the form view.

Initialization, #super and attributes

If you include

, your class automatically gets an
method that takes a params hash and calls setters for all attributes in the hash. If your model class inherits from a class that has its own
method that needs to get the super call, you should instead include
, which does not create an
method. Make your own
method, and in that you can assign the attributes using the
method and also call super with whatever args are needed.

Overriding the

If you name your model

, form params get passed to your controller in the
hash. As that's a bit ugly and perhaps doesn't play well with standing in for a real ActiveRecord model, Informal provides a method to override the model name.
class InformalCommand
  informal_model_name "Command"
  # ...

Note: the

feature is available only in Rails 3.1 or greater (unless somebody back-ports the required API change to 3.0.x).


The standard way that Rails generates ids for new records is to name them like

, as opposed to
for persisted records. I've found that when using informal models I often want more than one per page, and it's helpful to have a unique id for JavaScript to use. Therefore Informal uses the model's
to get a unique id for the record. Those ids in the DOM will look like
, which would be scary if you did anything with those memory addresses except use them for attaching scripts.


Copyright © 2011 Josh Susser. Released under the MIT License. See the LICENSE file.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.