# Params key

**URL:** <https://www.ruby-forum.com/t/params-key/133084>\
**Category:** Rails\
**Created:** [March 27, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/params-key/133084 "2008-03-27T16:19:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/params-key/133084/1 "2008-03-27T16:19:33Z")

</div>

Hi there,

What is the best way to see if a certain key exists in my params hash in  
my controller?

Thanks,  
steve

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/params-key/133084/2 "2008-03-27T16:28:17Z")

</div>

params.has\_key?(:the\_key)

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/params-key/133084/3 "2008-03-27T16:40:42Z")

</div>

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

> **[ApnaBill.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=apnabill.com)**
>
> Painless, quick delivery of your domain name. Fast and professional service.

On Thu, Mar 27, 2008 at 8:49 PM, Steve G. \<

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/params-key/133084/4 "2008-03-27T16:47:11Z")

</div>

Steve G. 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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/params-key/133084/5 "2008-03-27T16:50:45Z")

</div>

à¤®à¤¯à¤‚à¤• à¤œà¥ˆà¤¨ (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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 5:27pm UTC](https://www.ruby-forum.com/t/params-key/133084/6 "2008-03-27T17:27:06Z")

</div>

On Thu, Mar 27, 2008 at 9:18 PM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/params-key/133084/7 "2008-03-27T16:56:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 28, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/params-key/133084/8 "2008-03-28T16:08:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [March 27, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/params-key/133084/9 "2008-03-27T19:51:05Z")

</div>

Steve G. 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
