You may not still need this after James' response, but given the exact
"Parameters" in your email, you can get at the approval value for the
4th user with:
params["user"]["4"]["approval"]
params is a hash (well, in rails it's an enhanced hash, or "hash with
indifferent access" -- it just shrugs and says "meh") where the
values for some of the keys (commit, _method, etc) are just strings, but
the value for the "user" key is itself a hash, and the values of that
hash are also hashes, albeit one-element ones in your example.
If the all-in-one sort of approach like James suggested doesn't work
for you (and sometimes I will avoid it just to make things more readable
or so that I can get in and do more detailed processing) you can iterate
over the users:
params["user"].each do |user_id, value|
puts "User: #{user_id}, Approval: #{value['approval']}"
# or whatever checking, processing, and assigning you want
end
If you had more elements in each "user" hash and wanted to get at
all of them, you could put another iteration inside the above:
params["user"].each do |user_id, user_params|
user_params.each do |key, value|
# etc etc
end
end
Hope that helps.
-glenn
James Miller wrote:
> 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] <mailto:[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]
> <mailto:[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] <mailto:[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] <mailto:[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]
> <mailto:[email protected]>> wrote:
> > > >> Great, thanks Jason! I'll try it. - Cheri
> >
> > > >> On Sep 22, 12:04 pm, Jason King <[email protected]
> <mailto:[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
> <http://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]
> <mailto:[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 <http://user.id> %></td>
> > > >>>>>> <td><%= user.name <http://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
-~----------~----~----~----~------~----~------~--~---