The datalist_for plugin 10
Following in the Rails tradition, I created a simple solution to a problem I needed to solve, and I thought I’d extract it and share.
Here’s the scenario. You’ve got some simple tabular data. Some examples: order line items, flight status info, widget inventory, players on your corporate softball team. And you need a quick and dirty form to CRUD that data.
The datalist\_for plugin helps you do just that. Inspired by the excellent invoice line-item thingy in Blinksale, datalist\_for creates a table of data, complete with inline editing, and ajaxy create and delete. The resulting markup is very simple, and can be easily styled to suit your needs.
Usage
After installing the plugin, just include DatalistFor in your controller, and drop the datalist\_for tag into your form. So, your complete form might look like:
<%= form_tag "save" %> <%= datalist_for :monkeys, Monkey.find(:all) %> <%= submit_tag "Save" %> <%= end_form_tag %>
This will spit out a fully editable page of your favorite pet monkeys (and who doesn’t have pet monkeys). Then, in the target action of your form, do a save\_datalist to save the data. An example:
def save save_datalist flash[:notice] = "Your monkeys have been saved." redirect_to :action => 'whatever' end
You can also specify a subset of your model data:
<%= datalist_for :monkeys, Monkey.find_all_by_user_id(1), :create_with => { :user_id => 1 } %>
This will give you only monkeys for User 1. And any new monkeys will belong to User 1.
Other options:
<%= datalist_for :monkeys, Monkey.find_all_by_user_id(1), { :create_with => { :user_id => 1 },
:show_header => false,
:add_text => "Add a new monkey",
:exclude => [:likes_bananas, nickname],
:delete_content => image_tag "trash" } %>
This means:
- Don’t show the header row
- Use “Add a new monkey” as the link text for adding a new record (defaults to “Add modelname“)
- Exclude the :likes_bananas and :nickname columns
- Will use “trash.png” for row deletion instead of the default “Delete” text
###Demo
Here's the obligatory <a href="http://luckymonk.com/monkey">demo page</a>.
###Installation
Installing is straightforward:
ruby script\plugin install http://luckymonk.com/svn/repos/datalist_for
Exercises for the reader
datalist\_for suits my limited needs for the time being, but it’s far from perfect. Any ideas or suggestions are welcome. If you would like to help me write some of this stuff, even better. Here is my to-do/wishlist/known issues:
- Boolean types will generate a checkbox. A text type will create a text area. Everything else is a text field. There are no validations on say, dates, numeric fields, or any other types for that matter.
- It only shows your
content\_columns. A select tag for joins would be nice.
- You can’t have more than one datalist in a form. Don’t try it. You’ll be sad.
- Paging. There isn’t any.



Brian, this looks excellent. I can't wait to give this a shot in my side project tomorrow!
looks great for a quick fix for a database interface ..
but the Erb examples aren't displaying in Safari (might need to use the entities instead of the less-than/greater-than)
Sorry about that. Should be OK now. (Sadly, no Mac to test it on... yet)
Perhaps you could explain this:
"After installing the plugin, just include DatalistFor in your controller..."
a little better since there is NO documentation that I can find anywhere on what the heck this means. When I type "include DatalistFor" in my controller class just after the declaration, I get the error."uninitialized constant DatalistFor".
Nothing on the wiki that points out HOW to use plugins, just how to write them
Never mind, the problem was head slappingly obvious. Had to restart the server after I installed the plugin. (slap)
Maybe there needs to be a wiki entry with some troubleshooting tips for newbies to plugins.
http://luckymonk.com/svn/repos/datalistfor says "The requested URL /svn/repos/datalistfor was not found on this server."
Sorry about that Marius. It seems that our svn got corrupted somehow. We working on fixing it... will let you know when done.
Ok, everything is back to normal. Sorry for the inconvenience.
It works now. Thanks.
Hello...
Three comments:
if params[:datalistupdate].haskey?( "#{col.name}_#{id}" ) should be if params[:datalistupdate].haskey?( "#{col.name}_#{obj.id}" )
Cheers, Matthew