Hi there, What is the best way to see if a certain key exists in my params hash in my controller? Thanks, steve -- 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 -~----------~----~----~----~------~----~------~--~---
params.has_key?(:the_key) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Just print it out... pp params But a word of caution - do remove the pp line in production mode as this can bring your app down to its knees - "something went wrong"... In your view, you can do <%= debug(params) %> to see that what all''s there in the params hash. :) Maku http://www.apnabill.com On Thu, Mar 27, 2008 at 8:49 PM, Steve Glaz < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi there, > > What is the best way to see if a certain key exists in my params hash in > my controller? > > Thanks, > steve > -- > 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 -~----------~----~----~----~------~----~------~--~---
Steve Glaz wrote:> What is the best way to see if a certain key exists in my params hash in > my controller?params.has_key?(key) Now read your Ruby tutorial for a little while! And note that has_key? is sometimes only an intermediate solution, where you might really need the more compact expression: params.fetch(key, ''default value'') -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
मयंक जैन (makuchaku) wrote:> Just print it out... > > pp paramsDespite you may have answered the wrong question, it bears repeating here that nothing compares to this savage technique: raise params.inspect Bam - a browser full of nothing but your params. And a stack dump! -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the tips guys but I am not trying to debug so printing doesn''t help. I actually have conditional logic in my controller that depends upon whether or not the key is in the hash. So far the first solution seems to work. Phlip wrote:> मयंक जैन (makuchaku) wrote: > >> Just print it out... >> >> pp params > > Despite you may have answered the wrong question, it bears repeating > here that > nothing compares to this savage technique: > > raise params.inspect > > Bam - a browser full of nothing but your params. And a stack dump! > > -- > Phlip-- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Mar 27, 2008 at 9:18 PM, Phlip <phlip2005@gmail.com> wrote:> > मयंक जैन (makuchaku) wrote: > > > Just print it out... > > > > pp params > > Despite you may have answered the wrong question, it bears repeating here > that > nothing compares to this savage technique: > > raise params.inspect > > Bam - a browser full of nothing but your params. And a stack dump!I do agree that it dumps everything - but at times it helps in debugging... - but yes, point taken :) -- Maku --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Glaz wrote:> Thanks for the tips guys but I am not trying to debug so printing > doesn''t help. I actually have conditional logic in my controller that > depends upon whether or not the key is in the hash. So far the first > solution seems to work.Are you sure you shouldn''t just use two different routes? Sometimes routes are better than extra ?key=value&... parameters! -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you''re looking for one key in particular, you can always: ...do stuff... if params[:certain_key] Check for empty values if needed. -Kyle On Mar 27, 1:49 pm, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Steve Glaz wrote: > > Thanks for the tips guys but I am not trying to debug so printing > > doesn''t help. I actually have conditional logic in my controller that > > depends upon whether or not the key is in the hash. So far the first > > solution seems to work. > > Are you sure you shouldn''t just use two different routes? Sometimes routes are > better than extra ?key=value&... parameters! > > -- > Phlip--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---