My string of newbie questions continues! Thanks to all for being patient - you''ve been very helpful. I have the following code: def update_category update_key = @params[:update].keys #debugging output puts update_key puts @params[:advertiser_category][update_key] end I have the following structure in params: Parameters: {"advertiser_category"=>{"1"=>{"category_name"=>"Hunter Test Category"}, "2"=>{"category_name"=>"The Second Test Category"}}, "update"=>{"2"=>"Update"}} When I run this method, I see the following output: 2 nil The first line looks fine. The second line is giving me output I don''t expect. Params has a hash of hashes in it. I want to look at that hash and output the value of the key ''2''. You can see from the params that there is data there... Any idea why I''m getting nil? I would expect something like: category_nameThe Second Test Category Any help is appreciated.
On Friday 22 July 2005 23:28, Hunter Hillegas wrote:> I have the following code: > > def update_category > update_key = @params[:update].keys > #debugging output > puts update_key > puts @params[:advertiser_category][update_key] > end > > I have the following structure in params: > > Parameters: {"advertiser_category"=>{"1"=>{"category_name"=>"Hunter > Test Category"}, "2"=>{"category_name"=>"The Second Test Category"}}, > "update"=>{"2"=>"Update"}} > > When I run this method, I see the following output: > > 2 > nil > > The first line looks fine. The second line is giving me output I > don''t expect.Even though you don''t see it, update_key contains an array. You asked for keys and you got keys, not a single key. If instead of puts update_key you write p update_key or logger.debug("update_key: #{update_key.inspect}") you''ll see it. Thus, unless I''m mistaken, you ought to write def update_category update_keys = @params[:update].keys #debugging output puts update_keys puts @params[:advertiser_category][update_keys.first] end And better still, use logger.debug() Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
That did it... And thanks for the style pointers. This is quick and dirty throwaway code but it''s never too soon to start building good habits!> From: Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> > Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Fri, 22 Jul 2005 23:37:01 +0200 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: [Rails] Re: Extracting From Hash > > On Friday 22 July 2005 23:28, Hunter Hillegas wrote: > >> I have the following code: >> >> def update_category >> update_key = @params[:update].keys >> #debugging output >> puts update_key >> puts @params[:advertiser_category][update_key] >> end >> >> I have the following structure in params: >> >> Parameters: {"advertiser_category"=>{"1"=>{"category_name"=>"Hunter >> Test Category"}, "2"=>{"category_name"=>"The Second Test Category"}}, >> "update"=>{"2"=>"Update"}} >> >> When I run this method, I see the following output: >> >> 2 >> nil >> >> The first line looks fine. The second line is giving me output I >> don''t expect. > > Even though you don''t see it, update_key contains an array. You asked > for keys and you got keys, not a single key. If instead of > > puts update_key > > you write > > p update_key > > or > > logger.debug("update_key: #{update_key.inspect}") > > you''ll see it. Thus, unless I''m mistaken, you ought to write > > def update_category > update_keys = @params[:update].keys > #debugging output > puts update_keys > puts @params[:advertiser_category][update_keys.first] > end > > And better still, use logger.debug() > > Michael > > -- > Michael Schuerig Airtight arguments have > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. > http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails