This plugin enables the binding of other html elements to models, comparable
with form_for. The idea is that you can bind common html-elements to an object
or collection and that you can easily view the data. At the moment, it includes
definition lists and tables. For any suggestions, please send me a message.
This plugin works best with Haml,
which I strongly recommend. This plugin works fine with ERB though.
This plugin requires Rails 2.2 or higher!
I’ll start with an example, which explains the basic methods you can use:- definition_list_for @user do |dl| - dl.dt_and_dd :login do |login| = link_to(login, @user) - dl.dt_and_dd_h :name - dl.dt :email - dl.dd :email do |mail_address| = mail_to mail_address - dl.dt ‘some other value’ - dl.dd_h :signature
The dt method will automatically display the human_attribute_name of this
attribute (and thus translate it), the dd will automatically show the value
of this attribute. You can pass a block to these methods to play with the value
even more, like making it a link. Because these values may be harmful, a dd_h
method is also available, sanitizing html automatically. You can limit it to
one line, using dt_and_dd or dt_and_dd_h.
A similar option is available for tables.- table_for @users do |t| - t.head do - t.th :login - t.th :email - t.th :signature - t.body :sanitize => true - t.when_empty do |message| %em= message - t.when_not_empty do - t.tr do - t.td(nil, :colspan => t.width) do %em An extra row
By not specifying a block to t.body, it automatically loops all records,
using the columns specified in the t.head. Setting :sanitize to true will
automatically escape all html in the tds, but only when no block has been
With t.when_empty you can specify what will be placed in stead of the table
when the collection was empty. If you don’t specify anything, not even a block
it will render a default text, like “no entries found”. This is a translate
call, trying these scopes for the type:
And these scopes for the rest of the method (enabling interpolation of the
If the collection was empty, it doesn’t render a table at all. You can still
force a table to render, so you can fill it with your own data. Fill the :force
option with the class you’re using.
Now the when_empty renders a td with a colspan to match the width of the table.
The default translation scope of the message will be:
The model interpolation will be the human_name (thus translated) of the class.
If you do specify a block to t.body, you can define each cell, just as you
would do with the definition list. It automatically loops your data. The tr
will get a odd or even class automatically.
This plugin is still in development and is in now way considered to be used in
production. Please take care! If you’d like to contribute, please contact me!
Copyright © 2008 Iain Hecker, released under the MIT license