When dealing with multiple records like this or when needing to delete a bunch of records with checkboxes, I usually create two more actions for the resource: update_multiple and destroy_multiple. For updating multiple, you could do the following to handle all the users passed in your params[:user] hash:
users = params[:user] User.update(users.keys, users.values) On Wed, Sep 23, 2009 at 10:51 AM, liquid_rails <[email protected]>wrote: > > Thanks, but I still don't understand what is being submitted. Is it a > hash, an array of arrays, or what? For example, what would the syntax > be to retrieve the approval value of user "4"? > > > Parameters: {"commit"=>"Submit", "user"=>{"6"=>{"approval"=>"1"}, > "1"=>{"approval"=>"1"}, "2"=>{"approval"=>"2"}, "3"=> > {"approval"=>"2"}, "4"=>{"approval"=>"2"}, "5"=>{"approval"=>"2"}}, > "authenticity_token"=>"39757ec52a8cb0e04a7533686976d49388378c02", > "_method"=>"put", "action"=>"approve", "controller"=>"admin/users"} > > On Sep 23, 8:20 am, Nick Zadrozny <[email protected]> wrote: > > You also might consider the nested model approach, if all your users > belong > > to some common object. Might be a bit of a stretch for your case, but > works > > like a charm when it's a fit: > http://weblog.rubyonrails.org/2009/1/26/nested-model-forms > > > > > > > > On Tue, Sep 22, 2009 at 22:15, James Miller <[email protected]> > wrote: > > > > > Check out the ActiveRecord::Base#update method where you can pass an > > > array of IDs along with attributes to update all the user records at > > > once. > > > > > James > > > > > On Sep 22, 2009, at 10:12 PM, liquid_rails <[email protected]> > > > wrote: > > > > > > It works :) Awesome! > > > > Now I'm trying to figure out the best way to extract the data in the > > > > controller so that I can update the user.approval value for each > > > > user. This is what's being passed: > > > > > > Parameters: {"commit"=>"Submit", "user"=>{"6"=>{"approval"=>"1"}, > > > > "1"=>{"approval"=>"1"}, "2"=>{"approval"=>"2"}, "3"=> > > > > {"approval"=>"2"}, "4"=>{"approval"=>"2"}, "5"=>{"approval"=>"2"}}, > > > > "authenticity_token"=>"39757ec52a8cb0e04a7533686976d49388378c02", > > > > "_method"=>"put", "action"=>"approve", "controller"=>"admin/users"} > > > > > > Any hints would be appreciated! > > > > Thanks in advance, > > > > > > ~Cheri > > > > > > On Sep 22, 12:41 pm, liquid_rails <[email protected]> wrote: > > > >> Great, thanks Jason! I'll try it. - Cheri > > > > > >> On Sep 22, 12:04 pm, Jason King <[email protected]> wrote: > > > > > >>> No :) > > > > > >>> So, yeah, if you want just a single submit button then you'll want > a > > > >>> single form and you should use fields_for with the :index option... > > > > > >>> <% form_for ....etc. do %> <%# ....you can just have a > > > >>> form_tag here if you want %> > > > >>> <% @users.each do |user| %> <%# total aside: this is more > > > >>> Ruby- > > > >>> ish syntax %> > > > >>> <%= other things %> > > > >>> <% fields_for user, :index => user.id do |f| %> > > > >>> <%= f.radio_button :approval ..etc. %> > > > >>> <% end %> > > > >>> <% end %> > > > >>> <%= submit_tag %> > > > >>> <% end %> > > > > > >>> You'll find that all the radio buttons will be indexed, so you'll > > > >>> receive params[:user][id_of_user][:approval] in your controller, so > > > >>> you can easily set the right values for the right user all in one > > > >>> action. > > > > > >>> On Sep 22, 2009, at 11:48 AM, liquid_rails wrote: > > > > > >>>> Thanks, Jason. > > > > > >>>> So, if I have a separate form for each user, is there any way to > > > >>>> have > > > >>>> just one submit button to submit all forms at the same time? > > > > > >>>> Cheri > > > > > >>>> On Sep 22, 11:42 am, Jason King <[email protected]> wrote: > > > >>>>> The problem is that you've got form_for [:admin, :user] instead > of > > > >>>>> form_for [:admin, @user] and then you're expecting (somehow) > > > >>>>> f.radio_button to know which user to use to retrieve the seed > > > >>>>> value. > > > >>>>> Rails is using the last element of that array and calling > > > >>>>> the .approval method on it (hence the error about the user:Symbol > > > >>>>> (ie. :user ) not having that method defined. > > > > > >>>>> You're more likely to want to put the form within the @users > > > >>>>> iteration > > > >>>>> block, so that you have a separate form for each user. You can > > > >>>>> then do: > > > > > >>>>> <% for user in @users %> > > > >>>>> <% form_for [:admin, user] ...etc. do %> > > > >>>>> <%= f.radio_button :approval ...etc. %> > > > > > >>>>> If not, if you want it all in one form, then you won't be able > > > >>>>> to use > > > >>>>> the form_for block helpers, rather you'll need to use > > > >>>>> radio_button_tag > > > >>>>> and setup the values and names manually. > > > > > >>>>> Regards, > > > >>>>> Jason > > > > > >>>>> On Sep 22, 2009, at 11:30 AM, liquid_rails wrote: > > > > > >>>>>> I'm trying to generate a list of users with two radio buttons > > > >>>>>> next > > > >>>>>> to > > > >>>>>> each user where I can either approve or reject a user, all in > the > > > >>>>>> same > > > >>>>>> form. > > > >>>>>> The following code, illustrates what I am trying to do and the > > > >>>>>> error I > > > >>>>>> am getting. > > > >>>>>> Does anybody know how to do this? Thanks in advance! - Cheri > > > > > >>>>>> create_table "users", :force => true do |t| > > > >>>>>> t.string "name" > > > >>>>>> t.integer "approval", :default => 0 # 0=needs approval, > > > >>>>>> 1=approved, 2=not approved > > > >>>>>> end > > > >>>>>> ------------------- > > > > > >>>>>> <% form_for [:admin, :user], :url => {:action => > > > >>>>>> "user_approval" } do > > > >>>>>> |f| %> > > > >>>>>> <% for user in @users %> > > > >>>>>> <tr> > > > >>>>>> <td><%= user.id %></td> > > > >>>>>> <td><%= user.name %></td> > > > >>>>>> <td><%= user.approval %></td> #current state of the user's > > > >>>>>> approval status > > > >>>>>> <td><%= f.radio_button :approval,'1' %>"Approve" </td> > > > >>>>>> <td><%= f.radio_button :approval,'2' %>"Reject" </td> > > > >>>>>> </tr> > > > >>>>>> <% end %> > > > >>>>>> <p> <%= f.submit "Submit" %> </p> > > > >>>>>> <% end %> > > > > > >>>>>> --------- > > > >>>>>> Error: > > > >>>>>> undefined method `approval' for :user:Symbol > > > > -- > > Nick Zadrozny > > > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
