Tom Harrison
2007-Sep-22 21:49 UTC
Newbie: accessing form parameters in controller action?
Newbie alert! I am saving a form, and in my save action in the controller, I can do logger.info(params) and see a string in the log like "commitSaveactionsavecontrollerfoofooattrib1value1attrib2value2..." which is all of the info from my form, controller (foo), action (save), model (foo), and attribute values. It''s all there, but how do I get at it? I would like to get at the attribute values (form fields) discretely, and thought I could do something like logger.info(params[:attrib1]) which might return value1 But it doesn''t seem to return anything. Several other newbie attempts to query params fail too. What gives? Sorry to be a dunce. Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-22 22:03 UTC
Re: Newbie: accessing form parameters in controller action?
That should work ok. Try this to inspect them easier in your logs: logger.debug params.inspect That should help you I think. Tom Harrison wrote:> Newbie alert! > > I am saving a form, and in my save action in the controller, I can do > > logger.info(params) > > and see a string in the log like > > "commitSaveactionsavecontrollerfoofooattrib1value1attrib2value2..." > > which is all of the info from my form, controller (foo), action (save), > model (foo), and attribute values. It''s all there, but how do I get at > it? > > I would like to get at the attribute values (form fields) discretely, > and thought I could do something like > > logger.info(params[:attrib1]) > > which might return > > value1 > > But it doesn''t seem to return anything. Several other newbie attempts > to query params fail too. What gives? > > Sorry to be a dunce. > > Tom >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Harrison
2007-Sep-22 22:29 UTC
Re: Newbie: accessing form parameters in controller action?
Thanks William. So in a non-contrived case I logger.debug params.inspect returns {"commit"=>"Add Comment", "action"=>"save_comment", "controller"=>"survey", "survey"=>{"new_comment_body"=>"My Comment", "survey_id"=>"1", "new_comment_title"=>"My Title"}} None of these variants, starting one line later in the same method produces anything in the log (perhaps a newline?) logger.debug params[:new_comment_title] logger.debug "#{params[:new_comment_title]}" logger.debug "#{params[''new_comment_title'']}" I am missing something stupid ... but what? Thanks -- Dunce Boy Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Sep-22 22:47 UTC
Re: Newbie: accessing form parameters in controller action?
Tom Harrison wrote:> So in a non-contrived case I logger.debug params.inspect returns > > {"commit"=>"Add Comment", "action"=>"save_comment", > "controller"=>"survey", "survey"=>{"new_comment_body"=>"My Comment", > "survey_id"=>"1", "new_comment_title"=>"My Title"}} >new_comment_title is inside the survery hash: params[:survey][:new_comment_title] Fred -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-22 22:52 UTC
Re: Newbie: accessing form parameters in controller action?
Here is the deal, the key "new_comment_title" is actually in the hash pointed to by "survey" so you would access it like this: logger.debug params[:survey][:new_comment_title] Tom Harrison wrote:> Thanks William. > > So in a non-contrived case I logger.debug params.inspect returns > > {"commit"=>"Add Comment", "action"=>"save_comment", > "controller"=>"survey", "survey"=>{"new_comment_body"=>"My Comment", > "survey_id"=>"1", "new_comment_title"=>"My Title"}} > > None of these variants, starting one line later in the same method > produces anything in the log (perhaps a newline?) > > logger.debug params[:new_comment_title] > logger.debug "#{params[:new_comment_title]}" > logger.debug "#{params[''new_comment_title'']}" > > I am missing something stupid ... but what? > > Thanks -- > > Dunce Boy Tom > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Tom Harrison
2007-Sep-23 00:53 UTC
Re: Newbie: accessing form parameters in controller action?
William Pratt wrote:> Here is the deal, the key "new_comment_title" is actually in the hash > pointed to by "survey" so you would access it like this: > > logger.debug params[:survey][:new_comment_title]William & Fred -- Aha! (Doh!) Thanks. No, really, thanks. My wife thanks you, too, since I was obsessively trying different things that didn''t work. Tom -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---