Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mza/50076 to your computer and use it in GitHub Desktop.
Save mza/50076 to your computer and use it in GitHub Desktop.
## /users/index
#bulk-update-form
- for user in @users
- fields_for "user[]", user do |f|
= f.label :tryout
=radio_button("user", "status", "tryout")
= f.label :inactive
=radio_button("user", "status", "inactive")
= f.label :active
=radio_button("user", "status", "active")
= link_to_remote '[save]', :url => {:action => 'update' }, :submit => "bulk-update-form", :method => 'put'
## READ ME
I have a user model (restful_authentication) and would like a manager to be able to update the status of his users all at once. The users have three different statuses: active, inactive, guest. Guest is the default.
Using radio buttons I would like the manager to have free reign over how many users update at any one time.
There is a way to do this using loops, but that can get expensive quickly so I am looking for a better method.
# You can probably do the filtering with a select on your incoming parameters
# and then use update_all to perform the update in one hit.
#
# Probably best toncapsulate the update_all into the User model to keep the
# controller clean.
#
# This code hasn't been tested!
def update
@users = User.find(:all)
guests = ids_from params, "guest"
active = ids_from params, "active"
inactive = ids_from params, "inactive"
User.update_all "guest = true", "id IN (#{guests.join(",")}("
User.update_all "active = true", "id IN (#{active.join(",")}("
User.update_all "inactive = true", "id IN (#{inactive.join(",")}("
respond_to do |format|
flash[:notice] = 'Users Updated'
format.html do
render :update do |page|
#reload the page or the changes won't appear
page.reload
end
end
format.xml { head :ok }
end
end
private
def ids_from(parameters, key)
parameters["user"].keys.select { |id| params["user"][id][key] == "1" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment