dbbradle (owner)

Forks

Revisions

gist: 50061 Download_button fork
public
Public Clone URL: git://gist.github.com/50061.git
index.html.haml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## /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'
  
 
 
Update multiple records of a single model simultaneously
1
2
3
4
5
6
7
## 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.
users_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## users controller -- courtesy of @cnaths
 
#Something along these lines?
 
def update
  #grab all users from the database
  @users = User.find(:all)
  #parse the params array; loop through each user returning the id
  params["user"].keys.each do |id|
    #find the appropriate user record
    @user = User.find(id.to_i)
    #set the values as needed
    @user.guest = params["user"][id]["guest"]
    @user.active = params["user"][id]["active"]
    @user.inactive = params["user"][id]["inactive"]
    #save without validating, or add the appropriate invalidation handling
    @user.save(perform_validation = false)
  end
  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