Rather than have an
erbif you're a bad person ;) for every model you have, why not consolidate all of those into a single template? They all do the same thing anyway: render a table of objects along with a few actions. Using the wonderful
render_inheritablegem, you can define a single
index.html.xfile in a central location (such as
app/views/application) and have all of the customization occur at runtime. Thanks to
render_inheritable, the 4 index files for my 4 models can be reduced to this:
You'll need to define a
entry_classmethod in each of your controllers which returns the model's class. So in the
UsersControlleryou would have:
The same applies to the
_form.html.xfile. Using the wonderful
formtasticgem, creating a form which works for all of the models is as simple as this:
These are all pretty trivial and contrived; in any real application you wouldn't reuse the exact same view for all of your models. Where it becomes more useful is when crafting API responses, which tend to have more regular formats. Using the great
rablgem, you can create view templates for JSON responses like you would for HTML responses. Instead of mucking about with
to_jsonin your models or controllers, define the format of responses in a view, where it belongs.
render_inheritable, you can do some truly awesome things with your JSON (or XML) output. To create an index template which returns a per-model customizable collection of objects, simply create a file at e.g.
app/views/application/index.json.rablwith the following contents:
You'll need to define a function called
ApplicationControllerwhich returns e.g.
usersfor responses concerning
Userobjects endpoint or
groupsfor responses with
Doesn't look like much, but mix that together with
base.json.rablfiles for each one of your models, such as:
you now have a set of templates which render a collection of models, specialized for each class. That's pretty cool if you ask me.
Stay tuned for a way to render these customized API templates when errors are encountered, while still preserving the original error codes. It took me hours of digging through the Rails source to figure out.