Editing multiple records at once


#1

I want to be able to change a property on multiple records in my app. I
thought about adding a checkbox so that one can pick which records to
update. The question is how to implement the actual update - if the
checkbox isn’t part of the model, how can I iterate the records in the
view and see which are selected? My records are displayed using a
partial:

_product.html.erb

<%= check_box_tag ‘modify’ %>
<%=h product.name %>
<%=h product.price %>

index.html.erb

<% form_tag %>

<%= text_field_tag ‘price’ %>
<%= render :partial => ‘product’, @collection => @products %>
<%= submit_tag ‘save’ %>

<% end %>

Also, what kind of form should I put this in, and how to catch which
boxes are selected… I’m pretty clueless about this - thanks.


#2

I’ve decided on a different approach and I could use some help:

The page containing the records to modify also contains a small form to
filter the displayed records. It uses observe_form to periodically call
a function to filter the view.

I want to use this form filter as a selector on which records to update

  • it will be a two step process:
  1. the user picks which records to update by applying the filter.
  2. the user will update attributes using a different form.

The filter works by taking the selected attributes in it’s form and
calling a helper function that returns a :conditions array “cond” to use
with activerecord’s find function - @products = Product.find(:all,
:conditions => cond).

This is all fine, but I have one problem - how can get have the filter’s
conditions available to the updating function (which isn’t called from
the observe_form helper)?

If I could have the :conditions available when I search which records to
update, then everything will be simpler. Any advice will be more than
welcomed :slight_smile: