Update attributes for multiple objects

Hi everyone,

I've been thinking and googling about this for the whole afternoon and I
dont find the solution, but I know there must be an easy way (after all this
is rails):

I have a page where user objects are listed (e.g. customers) and they have
several attributes (like name, e-mail etc.) and a special attribute called
contacted (which can be true or false depending on if this customer has been
contacted by the sales people or not)

No big deal I thought, just write two controller methods 'set_contacted' and
'unset_contacted' and we are done by passing over the IDs of the
corresponding objects. But then there came in two more attributes and I
suddenly end up with 6 controller methods which feels horrible wrong to me
(Not even to mention the DRY principle violation).

What I would like to do is the following using checkboxes and a form for the
whole table and a submit button in order to save the changes made to the
user objects

User | contacted
user1 | [x]
user2 | [ ]
user3 | [ ]

[ Save changes ]

You sometimes see a similar behaviour in Webmail clients where you can check
serveral e-mails at once and then hit a delete button.

Any sort of help is appreciated! Have a nice week-end

Matt

P.S is it possible that submitting topics from the web interface of google
talks does not work?

Matthias von Rohr wrote:

What I would like to do is the following using checkboxes and a form for the
whole table and a submit button in order to save the changes made to the
user objects

User | contacted
user1 | [x]
user2 | [ ]
user3 | [ ]

[ Save changes ]

If you have multiple instances of the same model in a form, you can
refer to them like this:

<% for @user in @users -%>
  <%= check_box("user[]", "contacted") %>
  <%= check_box("user[]", "replied") %>
<% end -%>

In the controller, you'll find that params[:user] is a hash of hashes
that looks something like this:

{ 1 => {"contacted" => 1, "replied" => 1},
  2 => {"contacted" => 1, "replied" => 0},
  3 => {"contacted" => 0, "replied" => 0} }

where 1, 2 and 3 are the ids of the users.

You can then use that to update the user objects in one go:

User.update(params[:user].keys, params[:user].values)

Chris

Thanks a lot! Works like a charm!