Hi, I''ve diving into Rails and have a question about grabbing the values in a hash submitted by a form. Page 354 & 355 of the rails book touch on this but don''t go into much detail on picking those out with Ruby and I haven''t been able to figure it out on my own. For instance, using their example, if I wanted to look up user [name] I am trying something like: @name = params[:name] @results = Client.find(:all, :conditions => ["name = ?", name]) But params[:name] is returning the hash of {:name => "Dave"} How do I get the Dave out of that? I''m afraid I''m missing something terribly simple but I can''t find any examples of this kind of lookup. Any examples that might help me out as well? Tutorials, code, stuff I''ve missed in the Rails book? -- - Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I dont have the book handy, but from memory, I think the book examples push the form elements into a hash which can be accessed as a whole, or in part, ie, for your purposes: params[:name] returns a hash which might contain for example firstname/lastname, which are accessible like so: params[:name][:firstname] params[:name][:lastname] the purpose of the hash :name is to support the mass attribute set feature of rails, ie myname.attributes = params[:name] which would set :firstname and :lastname in myname from the params ________________________________ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Chris Cothrun Sent: Wednesday, 5 October 2005 9:27 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] pulling values from a form Hi, I''ve diving into Rails and have a question about grabbing the values in a hash submitted by a form. Page 354 & 355 of the rails book touch on this but don''t go into much detail on picking those out with Ruby and I haven''t been able to figure it out on my own. For instance, using their example, if I wanted to look up user [name] I am trying something like: @name = params[:name] @results = Client.find(:all, :conditions => ["name = ?", name]) But params[:name] is returning the hash of {:name => "Dave"} How do I get the Dave out of that? I''m afraid I''m missing something terribly simple but I can''t find any examples of this kind of lookup. Any examples that might help me out as well? Tutorials, code, stuff I''ve missed in the Rails book? -- - Chris _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Oct 4, 2005, at 4:27 PM, Chris Cothrun wrote:> Hi, > > I''ve diving into Rails and have a question about grabbing the > values in a hash submitted by a form. > > Page 354 & 355 of the rails book touch on this but don''t go into > much detail on picking those out with Ruby and I haven''t been able > to figure it out on my own. For instance, using their example, if I > wanted to look up user [name] I am trying something like: > > @name = params[:name] > @results = Client.find(:all, > :conditions => ["name = ?", name]) > > But params[:name] is returning the hash of {:name => "Dave"} > > How do I get the Dave out of that? I''m afraid I''m missing something > terribly simple but I can''t find any examples of this kind of lookup. >It sounds like you''re doubling the "name" in your form. It should look something like this: <%= text_field :user, :name %> And then in your controller: @name = params[:user][:name] Alternatively, (and assuming you are editing a user with your form, and that you have somewhere in your form a hidden input field named ''id'' with a value set to the id of the user) you can build a User object like so: @user = User.find params[:id] @user.update_attributes(params[:user]) And then save it with any new data that was just set: @user.save Duane Johnson (canadaduane)