You are here: Browse Railsplugins Attribute Fu
= AttributeFu =
Creating multi-model forms is amazingly easy with AttributeFu.
= Get It!
$ piston import http://svn.jamesgolick.com/attribute_fu/tags/stable vendor/plugins/attribute_fu
= Conventions
attribute_fu requires the fewest keystrokes if you follow certain conventions.
= Example
In this example, you’ll build a form for a Project model, in which a list of associated (has_many) tasks can be edited.
The first thing you need to do is enable attributes on the association.
class Project < ActiveRecord::Base
has_many :tasks, :attributes => true
end
Instances of Project will now respond to task_attributes, whose format is as follows:
@project.task_attributes = {
@project.tasks.first.id => {:title => "A new title for an existing task"},
:new => {
"0" => {:title => "A new task"}
}
}
Any tasks that already exist in that collection, and are not included in the hash, as supplied to task_attributes, will be removed from the association when saved. Most of the time, the form helpers should take care of building that hash for you, though.
Form Helpers
If you follow certain conventions, rendering your associated model’s form elements is incredibly simple. The partial should have the name of the associated element’s type, and look like a regular old form partial (no messy fields_for calls, or any nonsense like that).
<label>Title</label> <%= f.text_field :title %>
Then, in your parent element’s form, call the render_associated_form method on the form builder, with the collection of elements you’d like to render as the only argument.
That call will render the partial named _task.html.erb with each element in the supplied collection of tasks, wrapping the partial in a form builder (fields_for) with all the necessary arguments to produce a hash that will satisfy the task_attributes method.
You may want to add a few blank tasks to the bottom of your form; no need to do that in the controller anymore.
<%= f.render_associated_form(@project.tasks, :new => 3) %>
Since this is Web2.0, no form would be complete without some DHTML add and remove buttons. Fortunately, there are some nifty helpers to create them for us. Simply calling remove_link on the form builder in your _task partial will do the trick.
<label>Title</label> <%= f.text_field :title %> <%= f.remove_link “remove” %>
Creating the add button is equally simple. The add_associated_link helper will do all of the heavy lifting for you.
That’s all you have to do to create a multi-model form with attribute_fu!
= Updates
Come join the discussion on the {mailing list}[link:http://groups.google.com/group/attribute_fu]
Updates will be available {here}[http://jamesgolick.com/attribute_fu]
Creditsattribute_fu was created, and is maintained by {James Golick}[http://jamesgolick.com].
Copyright© 2007 James Golick, GiraffeSoft Inc., released under the MIT license
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly
Homepage: http://jamesgolick.com/2007/12/5/introducing-attributefu-multi-model-forms-made-easy